Examples:
873245
-34597862
5,280
(illegal because it contains a
comma)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 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:
class Foo{int x;float myMethod(){return x/2.0;}}has only the spaces that are absolutely necessary. A more readable version is ...
class Foo { int x; float myMethod() { return x/2.0; } }This illustrates several widespread programming conventions: putting the header of a class or method on one line, and its body on the next; indenting the body of a class or method farther than its header was indented; and lining up the opening and closing curly-braces of a class or method body.
class Foo { int x ; float myMethod ( ) { return x / 2.0 ; } }is technically legal, and defines exactly the same class, variable, and method, but it has so many spaces and line breaks and such misleading indentation that it's almost impossible to read.
Examples:
(3 + (4 * 5))
is legal.(3 + (4 * 5)
is illegal because there are more left parentheses than
right parentheses.class Foo { int x; float myMethod ) { return x/2.0; }is illegal because the right-parenthesis after
myMethod
is unmatched, and the left-curly-brace
after Foo
is unmatched.
Examples:
sqrt
Math
class that
computes square roots, but you could easily define a variable or
a method in a different class with the same name. square
true
final
keyword.
my age
myAge
class
if
, else
,
and return
.
Anything between two slashes //
and the end of the line is ignored
by Java (unless the slashes are inside a quoted string).
Examples:
// This is a comment that takes up the whole line.
int bignum 1234567;//a big number
int biggernum 123457890; // a really really
really big number
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:
int bignum/*a big number*/1234567;
int biggernum 123457890; /* a really really really big number */Note that the second line is still inside the comment, and will not produce an error message.
int biggernum 123457890; /* a really really big number int littlenum 7;Note that the comment is never ended, so the second line (which the programmer presumably meant to be Java code) will be treated as a comment too. Fortunately, BlueJ and many other Java development environments use color to show you what they consider to be comments.
"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."