• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What are literals in Java?

 
Ranch Hand
Posts: 182
1
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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  
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 182
1
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The thing I cannot understand is why

this :
is considered literal and why this:

flo  is not.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ioanna Katsanou wrote:... and why this:

 is not.



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

Henry
 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 182
1
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic