wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes KeyWords , ReservedWords , Literals - confused :( Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "KeyWords , ReservedWords , Literals - confused :(" Watch "KeyWords , ReservedWords , Literals - confused :(" New topic
Author

KeyWords , ReservedWords , Literals - confused :(

Satish Kumar
Greenhorn

Joined: Feb 18, 2005
Posts: 23
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

Joined: Aug 20, 2001
Posts: 1817

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.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

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

Joined: Feb 18, 2005
Posts: 23
Thanks lot for clarifying and also i have updated my profile
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: KeyWords , ReservedWords , Literals - confused :(
 
Similar Threads
Complete listing of JAVA Keywords
keywords and identifiers - Which is correct..
int as expression value
SIGNED AND UNSIGNED
Literals vs Keywords