File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes keyword vs reserved word Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "keyword vs reserved word" Watch "keyword vs reserved word" New topic
Author

keyword vs reserved word

Rao Raghu
Ranch Hand

Joined: Jan 05, 2007
Posts: 100
What is the difference between a key word and a reserved word.


RAGHU<br /> <br />"When the going gets tough, the tough get going"
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

JLS - 3.9 Keywords...
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.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
The way I understand it:
Reserved words are those words that the Java langauge restricts you from use as the names of variables, classes, methods etc.

Keywords are reserved words that have special meaning within the Java, such as abstract, class and private.

So while you use keywords to define your Java code you can NEVER use reserved words.

At least thats my understanding.
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
I would say they can be used interchangeably. Here's a link.
Rao Raghu
Ranch Hand

Joined: Jan 05, 2007
Posts: 100
Thank you all for your replies.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Quoted by Anupham Sinha:-
I would say they can be used interchangeably. Here's a link.
No, they are not synonymous. It says specifically on the list of keywords on that list that "null" "true" and "false" are "literals." That means they have symantic content; they actually represent a value.

A reserved word is one which can only be used in a pre-specified context.

Keywords control the flow and running of the programming; they are a little like operators in that respect.

All the keywords in Java are reserved words in the respect that they can only be used in the context they are designed for.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Quoted by Gavin Tranter:-
So while you use keywords to define your Java code you can NEVER use reserved words.
Yes, you do. You use the reserved words "null" "true" and "false" all the time.

What it means is that the two keywords "const" and "goto" which are in common use in the C and C++ languages were set as keywords when Java was developed. They are not used; the compiler throws an error if they are. The idea was to prevent people using those concepts at all.

[Edit for minor spelling errors]
[ March 08, 2007: Message edited by: Campbell Ritchie ]
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
Originally posted by Campbell Ritchie:
Quoted by Gavin Tranter:- Yes, you do. You use the reserved words "null" "true" and "false" all the time.


Not wishing to argue, but Sun defines "null" "true" and "false" as literals:
true, false, and null might seem like keywords, but they are actually literals;


In thier list of keywords, Sun do not list "null", "true" or "false" but they do list the reserved words "const" and "goto".
Keywords

So now I am more confused then ever, I think i will reread what I need to know about keywords for the SCJP. :S

G
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
It actually says in the JLS page you quoted ". . . are reserved for use as keywords."

In the sense that "true" "false" and "null" can only be used in the context they were designed for, they are "reserved" although the JLS doesn't say "reserved."

In most languages keywords are reserved words as well; Java is no exception. It says that "const" and "goto" are "reserved" without ever being used. As it says in the JLS page quoted, this is to allow for better error messages.

I think I actually agree completely with the first half of your post I quoted from.
And we can go on splitting hairs about what "reserved" means until the cows come home.
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

Did you ever see that WKRP in Cincinati episode where Artur Carlson throws the turkeys out of a helicopter? The timeless line was As God is my witness, I thought that Turkeys could fly.

Well, I'd like to add, As God is my witness, I thought that null, false and true were keywords.

Interesting thread.

-Cameron McKenzie
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: keyword vs reserved word