Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Literals vs Keywords

 
Bojan Knezovic
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the difference between literals and keywords - I have read that true, false and null are all literals and not keywords(I always thought the literals are - literals ) and I'm a bit confused here so if someone could shed some light...

Cheers,
B.
[ November 22, 2003: Message edited by: Bojan Knezovic ]
 
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JLS Chapters 3.9, 3.10:

The following character sequences, formed from ASCII letters, are reserved for use as keywords and cannot be used as identifiers (�3.8): [the list of keywords goes here]
The keywords const and goto are reserved, even though they are not currently used. This may allow a Java compiler to produce better error messages if these C++ keywords incorrectly appear in programs.
While true and false might appear to be keywords, they are technically
Boolean literals (�3.10.3). Similarly, while null might appear to be a keyword, it is technically the null literal (�3.10.7).

A literal is the source code representation of a value of a primitive type (�4.2), the String type (�4.3.3), or the null type (�4.1):
Literal:
IntegerLiteral
FloatingPointLiteral
BooleanLiteral
CharacterLiteral
StringLiteral
NullLiteral
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vad is right -- according to the JLS, "null", "true" and "false" are literals and not keywords.
BUT....
for the purposes of the exam -- don't worry about it.
From the SCJP 1.4 Exam objectives
Section 4: Language Fundamentals
* Identify all Java programming language keywords. Note: There will not be any questions regarding esoteric distinctions between keywords and manifest constants.
 
Bojan Knezovic
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vad, Jessica, thank you both. It's good to know these things they're fundamental anyway.

B.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic