This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

KeyWords , ReservedWords , Literals - confused :(

 
Satish Kumar
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I m really confused with the terminology of Reserved Words , Key Words & Literals . Wat does that each one of them mean exactly and wats the difference .
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Satish:

Welcome to JavaRanch! We don't have too many rules here, but one we do have is our JavaRanch Naming Policy. Please read it and then Change your display name to comply (we are looking for a first and a last name).

Thanks.
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as your confusion, here's the difference:

A Keyword is a word that has some semantic meaning to the Java compiler. You can find a list of keywords here.

A reserved word is a word that cannot be used to name variables. All keywords are reserved words. In addition, the words true, false, and null are reserved, since they are literal values.

A literal value is a value that is hard-coded into the code; it is the opposite of a variable. if you see

you see an if statement comparing the variable a to the literal value 10. null is a literal reference (note that is is not a literal object...), whilte true and false are literal booleans.
 
Satish Kumar
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks lot for clarifying and also i have updated my profile
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just for contrast, REXX is a language with keywords that have meaning in the right spot in the syntax but no reserved words. So you can write things like:

if if > then then then = if - then

The first if and the second then come where the interpreter expects keywords, the others are just fine as variables. I'll leave it to you to decide whether this is a good idea.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stan James:
Just for contrast, REXX is a language with keywords that have meaning in the right spot in the syntax but no reserved words. So you can write things like:

if if > then then then = if - then

The first if and the second then come where the interpreter expects keywords, the others are just fine as variables. I'll leave it to you to decide whether this is a good idea.


Ouch! That hurts my brain!

Layne
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joel McNary:
...A literal value is a value that is hard-coded into the code; it is the opposite of a variable. if you see

you see an if statement comparing the variable a to the literal value 10. null is a literal reference (note that is is not a literal object...), whilte true and false are literal booleans.


Just to expand further, Java allows literals for every primitive data type (i.e. int, long, float, double, etc.) and for Strings. A literal int is just a number:

1, 3, 4, 7, 9

You can also specify literal ints in hexidecimal (base 16) with a leading "0x":

0x0000, 0x1234, 0xFFCE

or octal (base 8) with a leading "0":

0010, 0755, 0213

Note that the first example (0010) is not 10. Since it starts with a 0, it is treated as an octal number that converts to the decimal number 8! If this is confusing, then I suggest you google for more information about number systems and converting between different bases.

If you need to store a number as a long, then you should typically append an int literal with "l" or "L":

111L, 123l

The capital version is preferred since it is easy to confuse a lower-case ell with a one.

A double literal is a numbers with decimal places:

3.14159, 1.4141

If you want to use float instead of double, append "f" or "F":

3.14159f, 1.4141F

The simplest type of char literal uses the single quote (') around any character:

'a', '?', ' '

You can also specify char literals using "\u" followed by the Unicode value in hexidecimal:

\u1234 \uF2E3

String literals are similar to char literals except you use double quotes ("):

"Hello, World!", "Mommy!"

I hope this gives you a better idea of what literals are. Please come back if you need any clarifications or any other questions.

Layne
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic