literals and keywords

S

Stefan Ram

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?
 
M

markspace

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.
 
S

Stefan Ram

markspace said:
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?
 
S

Stefan Ram

Chris Uppal said:
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.)
 
M

markspace

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.
 
A

Arne Vajhøj

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
 
A

Arne Vajhøj

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
 
S

Sigebriht

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

the true literal 26
Multicharacter literals 4
pre-PEP: Suite-Based Keywords 31
TF-IDF 1
7.0 wishlist? 321
Trying to wrap my head around futures and coroutines 0
l-values and r-values 28
Differences between C and C++ 100

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top