Legacy Java Syntax
Asked Answered
G

3

34

Reading the Java Code Conventions document from 1997, I saw this in an example on P16 about variable naming conventions:

int i;
char *cp;
float myWidth;

The second declaration is of interest - to me it looks a lot like how you might declare a pointer in C. It gives a syntax error when compiling under Java 8.

Just out of curiosity: was this ever valid syntax? If so, what did it mean?

Genipap answered 8/10, 2018 at 7:32 Comment(3)
Yes, that's C - must have been a copy/paste error.Gangling
this was never a valid syntaxMoule
Welcome to StackOverflow! I salute you - the one who reads the documentation!Cockleboat
K
25

It's a copy-paste error, I suppose.

From JLS 1 (which is really not that easy to find!), the section on local variable declarations states that such a declaration, in essence, is a type followed by an identifier. Note that there is no special reference made about *, but there is special reference made about [] (for arrays).

char is our type, so the only possibility that remains is that *cp is an identifier. The section on Identifiers states

An identifier is an unlimited-length sequence of Java letters and Java digits, the first of which must be a Java letter.
...
A Java letter is a character for which the method Character.isJavaLetter (§20.5.17) returns true

And the JavaDoc for that method states:

A character is considered to be a Java letter if and only if it is a letter (§20.5.15) or is the dollar sign character '$' (\u0024) or the underscore ("low line") character '_' (\u005F).

so foo, _foo and $foo were fine, but *foo was never valid.


If you want a more up-to-date Java style guide, Google's style guide is the arguably the most commonly referenced.

Kyongkyoto answered 8/10, 2018 at 7:45 Comment(6)
But in e.g. C++, * is part of the type specifier, not an identifier. I wouldn't expect the C++ spec to mention * in the section on identifiers.Lynnet
@Lynnet True, but given that the example lists char *cp, char is necessarily a type (because it's a keyword), so *cp is necessarily an identifier (as per 14.3). It's impossible to point to a single point in the spec which says that * has no special meaning (other than being the multiplication operator), so not possible to cite it.Kyongkyoto
Going even further back in time, the Oak Specification did not mention * and pointers (explicitly) either.Cesya
@Marco13: That's an interesting read. I didn't know Oak had support for Design-by-Contract and that it supported Uniform Access. Also, the public-key signed packages with access restrictions look interesting for a language that is used for running of untrusted code from the network.Jewbaiting
Interestingly you can still get a printed version of JLS 1.0 (ISBN-10: 0201634511) amazon.com/Java-TM-Language-Specification/dp/0201634511 - dl.acm.org/citation.cfm?id=560667Ytterbium
@Ytterbium A literal paperweight :)Kyongkyoto
P
5

It appears that this is a generic coding style document for C-like languages with some Java-specific additions. See, for example, also the next page:

Do not use the assignment operator in a place where it can be easily confused with the equality operator. Example:

if (c++ = d++) { // AVOID! Java disallows.
    …
}

It does not make sense to tell a programmer to avoid something that is a syntax error anyway, so the only conclusion we can draw from this is that the document is not 100% Java-specific.

Another possibility is that it was meant as a coding style for the entire Java system, including the C++ parts of the JRE and JDK.

Note that Sun abandoned the coding style document even long before Oracle came into the picture. They restrained themselves to specifying what the language is, not how to use it.

Porcupine answered 8/10, 2018 at 11:41 Comment(0)
N
0

Invalid syntax!

It's just a copy/paste mistake.

The Token (*) in variables is applicable only in C because it uses pointers whereas JAVA never uses pointers.

And Token (*) is used only as operator in JAVA.

Nightdress answered 11/10, 2018 at 9:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.