Legacy Java Syntax

  • A+
Category:Languages

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?

 


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.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: