• Post Reply Bookmark Topic Watch Topic
  • New Topic

What are literals in Java?  RSS feed

 
Ioanna Katsanou
Ranch Hand
Posts: 93
1
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I was studying a little bit about literals in Java.. What are literals in Java? I did a Google search and from the first results that poped out I understood that literal  represent "constant values" For example:


or


I came along to this question:

Which of the following statements contain literal values?



Thanks in advance for the help 
 
Paul Clapham
Sheriff
Posts: 22211
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ioanna Katsanou wrote:I understood that literal  represent "constant values"


Yes. That's a very good way of thinking about it. But more precisely, they are constant values which are recognized by the compiler as such because they are expressed in special formats. So that includes numeric literals, string literals, boolean literals, character literals, and the null literal. (I think that's all... there might be something I've missed but you should get the idea from that list.)

So no, variable names are not literals, even though you might be able to analyze the code and decide that the value of the variable cannot change. Likewise computed expressions like 8/2 and "ice-" + "cream" are not literals, even though their result is a constant value.
 
Ioanna Katsanou
Ranch Hand
Posts: 93
1
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The thing I cannot understand is why

this :
is considered literal and why this:

flo  is not.
 
Campbell Ritchie
Sheriff
Posts: 54076
130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ioanna Katsanou wrote:The thing I cannot understand is why

this :
is considered literal  . . .
There are no literals in that piece of code.
 
Henry Wong
author
Sheriff
Posts: 23029
120
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ioanna Katsanou wrote:... and why this:

  is not.


And ... the "10" in that expression is a literal.

Henry
 
Fred Kleinschmidt
Bartender
Posts: 517
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A more interesting question would have been whether there is a literal here:

So for the OP, what do you think?
 
Ioanna Katsanou
Ranch Hand
Posts: 93
1
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Kleinschmidt wrote:A more interesting question would have been whether there is a literal here:

So for the OP, what do you think?


According to my understanding true and false in boolean values are literals. Is that correcT?
 
Campbell Ritchie
Sheriff
Posts: 54076
130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ioanna Katsanou wrote:. . . true and false in boolean values are literals. Is that correcT?
The Java® Language Specification (=JLS) shou‍ld answer that question. That is an unusual part of the JLS: it is easy to understand.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!