Spelling Rules of Java

(Note to computer-language purists: what I'm calling "spelling" is approximately what language geeks call "lexical structure", albeit with some semantics as it seems to come up naturally.)

Outline


Numbers
integers
a sequence of as many digits (0-9) as you wish. You can also put a plus ("+") or minus ("-") sign in front, with no spaces in between. If the number is larger than about 2000000000 (or smaller than about -2000000000) Java may not handle it correctly.

Examples:

  • 873245
  • -34597862
  • 5,280 (illegal because it contains a comma)

floating-point numbers
a sequence of digits, a period, and another sequence of digits, with no spaces in between. You can also use scientific notation by putting the letter "e" or "E" and a positive or negative integer at the end, with no spaces in between.

Examples:

  • 3.1415926
  • 6.02e23
  • -.738E-17

Notes: Such numbers are usually inexact: for example, 49.0 *  (1.0 / 49.0) will probably be very close, but not exactly equal, to 1.0. They are limited in precision -- only about fifteen digits after the decimal point, and the exponent can be no larger than about 308 (depending on what's in front of the decimal point)

Spaces and line breaks

Spaces and line breaks separate one name, number, or keyword from another. In most places in the Java language they're not required, as there's a punctuation mark of some kind to clearly distinguish one word from another. The most common place that spaces are required is in a declaration, between the type and the name of the variable or method. A space is similarly required in a class header, between the word class and the name of the class.

In general, a line break can be anywhere that a space can be, except inside a quoted string. Spaces and line breaks are allowed between any two names, numbers, keywords, operators, etc. but they are not allowed inside a name, number, keyword, or operator.

Examples:

Parentheses, brackets, and braces
Left parentheses must match right parentheses. Left square brackets must match right square brackets. Left curly braces must match right curly braces. Simple enough?

Examples:

Names
Methods, classes, variables, and keywords all follow basically the same spelling rules. They can be made up of upper-case and lower-case letters (yes, case matters!), underscores, and a few other punctuation marks. They cannot contain spaces or hyphens. Nor can they contain parentheses, curly braces, square brackets, apostrophes, commas, or quotation marks, as these all have special meanings in Java.

Examples:

Comments

Anything between two slashes // and the end of the line is ignored by Java (unless the slashes are inside a quoted string).
Examples:

Anything between slash-star /* and star-slash */ is ignored (unless the comment delimiters are inside a quoted string). This kind of comment can extend over several lines.
Examples:

Strings
A string is any sequence of characters surrounded by "double-quote marks". Spaces, upper and lower case letters, and any punctuation mark are allowed inside. If you want to put a double-quote mark inside a string, precede it with a backslash, e.g.
"This is a \"string\" containing quotation marks."
If you want to put a backslash inside a string, precede it with a backslash, e.g.
"This is another string containing a single \\ backslash."

A string cannot extend from one line of source code to the next. If you want a line break inside a string, use "\n" to represent it.

"This is a\nstring containing\ntwo line breaks."


Last modified: Mon Oct 21 09:57:33 EDT 2002
Stephen Bloch / sbloch@adelphi.edu