wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Basic question on foundational Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Basic question on foundational Java" Watch "Basic question on foundational Java" New topic
Author

Basic question on foundational Java

Olakunle Oladipo Oni
Greenhorn

Joined: Aug 28, 2008
Posts: 18
The term "Java keywords" and "Java reserved words" to me seem to be used synonymously sometimes. What is the real difference between them? What makes a word "reserved" and not still considered a "keyword". Or can the two words apply in most instances?I'll appreciate a clear cut explanation with examples from both sides using the Java programming language as a reference point.

Olakunle oladipo oni




Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4460
    
    8

I don't think there is a difference. The Java Language Specification calls them keywords, some others might call them reserved words. One case where you could use different terms (but the JLS doesn't) are const and goto, which are "reserved", but are not actually used by the language.

Edit: there's also the boolean literals true and false, and the null literal null. The JLS doesn't call them keywords, but you can't used them as an identifier so you could call them "reserved".
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39784
    
  28
Keywords are part of the syntax of a language, so they are devoid of semantic content. You cannot get a sensible answer to questions like
  • What is the semantic content of int?
  • How much is an if?
  • Can I assign else to something?
  • You can however do this for literals. They have semantic content. I can say what the semanic content of "Campbell" is, or whether I can assign 123 to something, or how much 456.789 is.
    You can say
  • false has semantic content. Not a lot, but it is there.
  • How much is null? Nothing at all, but that is a real answer.
  • You can definitely assign true to something.
  • Obviously there are restrictions on their use.

    Is the word double in Java™ a keyword or a reserved word? Answer: both. A keyword is one which has a definite meaning in the language, but no semantic content. A reserved word is one which cannot be used in any other context. The words false, true and null have semantic content, but can only be used in a particular context.

    Actually, I am beginning to wonder whether the keywords this and super are really keywords; when you say this.foo or super.bar(), they are behaving rather like literals. Similarly writing public class Foo uses class as a keyword, but in Foo.class it is has got a bit of semantic contant. So let’s say they are on the borderline.
    But the Java Language Specification definitely calls them keywords.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39784
        
      28
    You would need to verify this, but I have heard of languages, eg older versions of FORTRAN, where keywords were not reserved, so you could use IF as an identifier.
    Matthew Brown
    Bartender

    Joined: Apr 06, 2010
    Posts: 4460
        
        8

    Campbell Ritchie wrote:A reserved word is one which cannot be used in any other context. The words false, true and null have semantic content, but can only be used in a particular context.


    That's an interesting distinction - I hadn't thought of that. I was running a tutorial a few weeks ago where, as a refresher, I got people to write down as many Java keywords as they could in 5 minutes (quite a fun challenge - most people miss some really obvious ones). I decided I would accept true, false and null as I decided I couldn't give a good justification why they weren't keywords (other than "the JLS says so").

    Actually, I am beginning to wonder whether the keywords this and super are really keywords; when you say this.foo or super.bar(), they are behaving rather like literals.

    I suppose you could say they have relative, but not absolute, semantic content.
    Joanne Neal
    Rancher

    Joined: Aug 05, 2005
    Posts: 3737
        
      16
    Campbell Ritchie wrote:You would need to verify this, but I have heard of languages, eg older versions of FORTRAN, where keywords were not reserved, so you could use IF as an identifier.

    Java had the same problem with enum which only became a keyword as of 1.5


    Joanne
    Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    Joanne Neal wrote:
    Campbell Ritchie wrote:You would need to verify this, but I have heard of languages, eg older versions of FORTRAN, where keywords were not reserved, so you could use IF as an identifier.

    Java had the same problem with enum which only became a keyword as of 1.5


    assert also, I think.
    Matthew Brown
    Bartender

    Joined: Apr 06, 2010
    Posts: 4460
        
        8

    Jeff Verdegan wrote:assert also, I think.

    Yes. Version 1.4. And also strictfp (version 1.2). Which, I think, is the only (functioning) Java keyword I've never actually used.
    Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    Other languages and standard definitions aside, the JLS says that these are the 20 keywords in the language:
    http://docs.oracle.com/javase/specs/jls/se5.0/html/lexical.html#229308

    The index makes no mention of "reserved words," but the linked page also says,
    The keywords const and goto are reserved, even though they are not currently used.
    and
    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).


    So I'd say that as far as Java is officially concerned, it looks like they're synonymous, but I would agree that I'd true, false, and null are reserved words, even though the JLS doesn't seem to call them out as such.
    Kotto Bass
    Greenhorn

    Joined: Jan 27, 2006
    Posts: 17
    My understanding is, keywords are a subset of reserved words and are used as identifiers. Reserved words also include literals. Also, keywords are currently in use and have meaning, but not all reserved words are in use. Words like 'goto' currently have no meaning in Java (are not keywords), but have been reserved and may or may not become a keyword down the road. In the example above "The keywords const and goto are reserved", we are not referring to Java 'keyword', but to grammar 'keyword' as used in the English language, as it is preceded by 'The'.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39784
        
      28
    Kotto Bass wrote:My understanding is, keywords are a subset of reserved words and are used as identifiers. Reserved words also include literals. Also, keywords are currently in use and have meaning, but not all reserved words are in use. Words like 'goto' currently have no meaning in Java (are not keywords), but have been reserved and may or may not become a keyword down the road. In the example above "The keywords const and goto are reserved", we are not referring to Java 'keyword', but to grammar 'keyword' as used in the English language, as it is preceded by 'The'.
    Not at all convinced. I think you have missed out a “not” somewhere towards the beginning of that post.
    No, keyword is not a word used about grammars in common English, and words preceded the “the” are not called keywords; they sound more like nouns.
    Keywords have syntactic meaning rather than semantic, but as I said yesterday, some of them are sort of borderline.
    The words goto and const are keywords, but never used. I think you have misunderstood the whole discussion to date; please go back and read it all again.

    The Java™ Language Specification says keywords are reserved, but doesn’t use the word “reserved” of null true and false.
    Kotto Bass
    Greenhorn

    Joined: Jan 27, 2006
    Posts: 17
    Campbell Ritchie wrote:
    Kotto Bass wrote:My understanding is, keywords are a subset of reserved words and are used as identifiers. Reserved words also include literals. Also, keywords are currently in use and have meaning, but not all reserved words are in use. Words like 'goto' currently have no meaning in Java (are not keywords), but have been reserved and may or may not become a keyword down the road. In the example above "The keywords const and goto are reserved", we are not referring to Java 'keyword', but to grammar 'keyword' as used in the English language, as it is preceded by 'The'.
    Not at all convinced. I think you have missed out a “not” somewhere towards the beginning of that post.
    No, keyword is not a word used about grammars in common English, and words preceded the “the” are not called keywords; they sound more like nouns.
    Keywords have syntactic meaning rather than semantic, but as I said yesterday, some of them are sort of borderline.
    The words goto and const are keywords, but never used. I think you have misunderstood the whole discussion to date; please go back and read it all again.

    The Java™ Language Specification says keywords are reserved, but doesn’t use the word “reserved” of null true and false.


    Wow, after reading around, I now feel enlightened (Thanks Campbell). The JLS makes no clear effort in outlining the difference. I have been under the impression that 'goto' and 'const' are not Java Keywords - apparently, there is a lot of misinformation out there: http://stackoverflow.com/questions/1078908/what-is-the-difference-between-keyword-and-reserved-word. If it could be summarized: Reserved words = Keywords + Literals (true, false, null). Correct?
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39784
        
      28
    You need to be very careful when writing posts like that SO post, otherwise you can post confusing information.
    Kotto Bass
    Greenhorn

    Joined: Jan 27, 2006
    Posts: 17
    Campbell Ritchie wrote:You need to be very careful when writing posts like that SO post, otherwise you can post confusing information.
    Not clear, but was that in response to the last post, sheriff? Again folks apologies for any confusion on my original post above, after a long week. We are all here to see Java thrive.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39784
        
      28
    I probably wasn’t clear. What I meant was, whoever wrote that SO post was not clear about it.
    Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    Campbell Ritchie wrote:You need to be very careful when writing posts like that SO post, otherwise you can post confusing information.


    You mean where I said:
    Me wrote:So I'd say that as far as Java is officially concerned, it looks like they're synonymous, but I would agree that I'd true, false, and null are reserved words, even though the JLS doesn't seem to call them out as such.

    ?

    Sorry for any confusion I may have caused. All I'm saying is the JLS only explicitly defines "keywords." It uses the word "reserved" almost in passing, but in a way that seems synonymous with its use of the term "keyword". HOWEVER, in the absence of the JLS having a specific definition for "reserved words", I think it's reasonable to apply a "common usage" definition and say that true, false, null are reserved, in the sense that they are pat of the syntax of the language and not available to us as identifiers?

    Any better?
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Basic question on foundational Java