1

I found the following statement about the Literal concept in the Oracle Java Tutorial:

A literal is the source code representation of a fixed value; literals are represented directly in your code without requiring computation

I understand that int * int or String + String ... isn't a literal. So I try this code:

int num1 = 3*5;
int num2 = 15
System.out.println(num1==num2);// it print true

My question: 3 * 5 in my code is a literal or not? and why? Thanks!

user207421
  • 305,947
  • 44
  • 307
  • 483
PhongVH
  • 51
  • 8
  • 2
    3*5 is not a literal because it requires computation... what's the problem? – Andrew Li Mar 15 '17 at 03:47
  • I don't know about java specifically, but `num1` would definitely be a literal/constant after compilation in most sane compilers. It would get compiled into `int num1 = 15` because there's no reason to waste cycles on this at runtime. – caesay Mar 15 '17 at 03:50
  • 1
    aren't you saying both sides yourself? .... i understand that int * int or String + String... isn't a literal ............ My question: 3 * 5 in my code is a literal or not?......... you are saying it isn't a literal and asking if it a literal?? – Abhishek Mar 15 '17 at 03:50
  • `3*5` is not going to be a literal because the compiler will carry out the computation and store the result. Before that though the compiler does process two literals `3` and `5`. `15` is a literal in the second line. – markspace Mar 15 '17 at 04:02
  • @markspace: Thanks for your answer – PhongVH Mar 15 '17 at 04:19
  • @Abhishk : Becauce I see question http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java . In this said: " when you create any String literal, the JVM first searches for that literal in String pool, and if it finds a match, that same reference will be given to the new String" . So I don't understand why num2 is a literal and num1 is not, num1==num2 return true. – PhongVH Mar 15 '17 at 04:43
  • @phongvan Strings are treated specially in Java, so you should read up on that somewhat separately from this more basic question about the language overall. You're also mixing the concepts of "value" with "representation of value." – Brick Mar 15 '17 at 05:30

1 Answers1

1

The comments on your question are mixing concepts. The definition is clear, 3 and 5 are literal, but 3*5 is not since it requires a computation.

It is true that the compiler will likely do that computation at compile time because the result is a constant knowable at compile-time, but it does not replace it with a literal. See the definition that you cite. A literal is the source code representation. If the compiler does do that operation at compile-time, it will impact the byte code, not the source code.

The comment that you're making about num1==num2 is completely irrelevant to the question. Again see the definition. The literal is defined as a representation of value, where equality is a statement about the value itself.

Brick
  • 3,998
  • 8
  • 27
  • 47
  • I'd like to add that the term "literal" means just that, "literal", as in that the source code literally and explicitly says `15`, not expressed in another way, such as `3*5`. The word "literally" means "as the letters say". – Thomas Padron-McCarthy Mar 15 '17 at 07:16