PhongVH
PhongVH

Reputation: 51

"Literals are represented directly in your code without requiring computation". What does this mean?

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!

Upvotes: 1

Views: 135

Answers (1)

Brick
Brick

Reputation: 4262

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.

Upvotes: 1

Related Questions