literals and keywords

Discussion in 'Java' started by Stefan Ram, Dec 28, 2013.

  1. Stefan Ram

    Stefan Ram Guest

    The JLS contains this wording:

    »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).«

    Does it matter?

    Can you find any program, where one can observe the difference?
    E.g., some program that does compile because they are not keywords,
    but would not compile when they were keywords, or vice versa?
    Or are there any other observable consequences of this difference?
    Stefan Ram, Dec 28, 2013
    #1
    1. Advertising

  2. Stefan Ram

    markspace Guest

    On 12/28/2013 8:15 AM, Stefan Ram wrote:
    > Does it matter?
    >
    > Can you find any program, where one can observe the difference?
    > E.g., some program that does compile because they are not keywords,
    > but would not compile when they were keywords, or vice versa?
    > Or are there any other observable consequences of this difference?
    >


    Yes, it matters, or they wouldn't make a distinction. We talked about
    this already: literals can be used in expressions, keywords can't. The
    observable difference is in the implementation of the JLS.

    <http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.8>

    You can write "int i = 1 + 2;" but you can't write "Object o = for +
    try;" because that section of the JLS says so. Read it.
    markspace, Dec 28, 2013
    #2
    1. Advertising

  3. Stefan Ram

    Stefan Ram Guest

    markspace <> writes:
    >this already: literals can be used in expressions, keywords can't.


    It is not true that keywords can't be used in expressions,
    for example, the expression »this« contains the keyword »this«.

    Keywords can introduce statements, as in the case of »if«,
    or they can name types, as in the case of »int«, or
    references as in the case of »this«. It would have been
    possible to make »true« a keyword in Java as well. Just as
    the keyword »this« names a value, the keyword »true« would
    name a value. So, when they have decided to call »this« a
    keyword, but not »true«, does it matter? Is there any point
    where it helps to know that »this« is a keyword, but »true«
    is not?
    Stefan Ram, Dec 28, 2013
    #3
  4. Stefan Ram

    Stefan Ram Guest

    "Chris Uppal" <-THIS.org> writes:
    >I think the distinction is of more use to compiler (and other tool) creators[*]
    >than to common-or-garden programmers.


    I was trying to get the general meaning of the concept of
    »keyword« in Java. Except than listing all keywords, can
    we give a shorter wording for the concept of a keyword
    that includes »this«, but excludes »true«?

    In the meantime, I have found the answer. Readers can think
    about it themself, should they wish to do so, and then read
    the ROT13:

    N xrljbeq vf nal jbeq gung vf cneg bs gur flagnpgvpny
    tenzzne (nf tvira va puncgre 18). »guvf« vf pbafvqrerq gb or
    n cneg bs guvf »flagnpgvpny tenzzne« bs Wnin, juvyr »gehr«
    vf abg. »gehr« vf pbafvqrerq gb or n cneg bs gur »yrkvpny
    tenzzne« nf tvira va frpgvba 2.4: »gur qrsvavgvba:
    ObbyrnaYvgreny: bar bs gehr snyfr [vf] n yrkvpny tenzzne
    cebqhpgvba« (V punatrq »va« gb »vf« va gur dhbgngvba.)
    Stefan Ram, Dec 28, 2013
    #4
  5. Stefan Ram

    markspace Guest

    On 12/28/2013 9:17 AM, Stefan Ram wrote:
    > markspace <> writes:
    >> this already: literals can be used in expressions, keywords can't.

    >
    > It is not true that keywords can't be used in expressions,
    > for example, the expression »this« contains the keyword »this«.
    >


    Yes, and there are two other keywords mentioned in that section that get
    used in expressions, if you read it.

    > Keywords can introduce statements, as in the case of »if«,


    This is rather untrue. if takes an expression as an argument because
    the spec says there must be an expression there. These things aren't
    founded out some inherent principle of ur-computing, they're arbitrarily
    dictated by the the spec. That's it.

    > or they can name types, as in the case of »int«, or


    "int" when it introduces an expression is called a "declaration", not an
    expression. "(int)" as a cast is a special type of expression. Both of
    those constructs are dictatated by the spec.

    > references as in the case of »this«. It would have been
    > possible to make »true« a keyword in Java as well. Just as


    But it doesn't make sense. When you look at discreet math, the concept
    of a literal value is already present. "true" and "false" are the
    literal values that boolean values can have in mathematics.

    When the spec says that true and false are literals, it's trying to
    mimic the natural language way of expressing that concept, what already
    exists in the math. What you seem to be saying to me is contorted and
    unnatural. Just read that part of the spec the way it's written, and
    understand it naturally.

    Why exactly "this" and "new" and "super" aren't literals, I don't know.
    Possibly because their value isn't fixed, but depends on context, and
    therefore isn't actually a single literal value. They represent a range
    of values, and the language implementers didn't want to call them
    operators or variables or introduce a new concept like "special
    function," so they just called them a keyword because they had the
    concept already and it worked well enough.
    markspace, Dec 28, 2013
    #5
  6. Stefan Ram

    Arne Vajhøj Guest

    On 12/28/2013 1:56 PM, markspace wrote:
    > Why exactly "this" and "new" and "super" aren't literals, I don't know.
    > Possibly because their value isn't fixed, but depends on context, and
    > therefore isn't actually a single literal value. They represent a range
    > of values, and the language implementers didn't want to call them
    > operators or variables or introduce a new concept like "special
    > function," so they just called them a keyword because they had the
    > concept already and it worked well enough.


    this and super are not constant so they could never be called a literal.

    And new is an operator. More like instanceof.

    Arne
    Arne Vajhøj, Dec 28, 2013
    #6
  7. Stefan Ram

    markspace Guest

    On 12/28/2013 11:53 AM, Arne Vajhøj wrote:

    > And new is an operator.


    Is it? That section I linked to refers to "new" as a keyword.

    <http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.9>

    Five paragraphs after that link.

    "...the type arguments to the constructor immediately follow the keyword
    _new_."

    And a couple of other places too.
    markspace, Dec 28, 2013
    #7
  8. Stefan Ram

    Arne Vajhøj Guest

    On 12/28/2013 3:41 PM, markspace wrote:
    > On 12/28/2013 11:53 AM, Arne Vajhøj wrote:
    >
    >> And new is an operator.

    >
    > Is it? That section I linked to refers to "new" as a keyword.
    >
    > <http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.9>
    >
    > Five paragraphs after that link.
    >
    > "...the type arguments to the constructor immediately follow the keyword
    > _new_."
    >
    > And a couple of other places too.


    Operators that are words and not special characters are also keywords.

    But a quick check reveals that you are absolutely right - JLS do not
    call new an operator even though it does call instanceof an operator.

    That surprises me. In C++ and C# new is considered an operator.

    And I do not seem to be the only one surprised. The Java tutorial
    actually call it an operator.

    Arne
    Arne Vajhøj, Dec 28, 2013
    #8
  9. Stefan Ram

    Sigebriht Guest

    On 29/12/2013 03:14, Patricia Shanahan wrote:

    > I suspect that not also classing them as keywords might be intended to
    > keep classes of tokens disjoint. Each token is either a keyword, or a
    > literal, or white space, etc. but cannot be more than one of those things.


    That certainly makes sense.
    Sigebriht, Jan 7, 2014
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Goche
    Replies:
    8
    Views:
    16,424
  2. Replies:
    3
    Views:
    20,891
    Roedy Green
    Jan 21, 2006
  3. Jamie Allison

    Mete Keywords and Description

    Jamie Allison, Jun 22, 2006, in forum: HTML
    Replies:
    3
    Views:
    408
    PeterMcC
    Jun 23, 2006
  4. Andr? Roberge
    Replies:
    8
    Views:
    328
    Sridhar R
    Oct 9, 2004
  5. Berteun Damman

    pyparsing and 'keywords'

    Berteun Damman, Dec 14, 2004, in forum: Python
    Replies:
    3
    Views:
    410
    Paul McGuire
    Dec 14, 2004
Loading...

Share This Page