The U.S. judicial system has a principle that the accused is "presumed innocent until proven guilty." Java, like most programming languages, uses the opposite approach: a piece of alleged Java code is presumed guilty (i.e. illegal) until proven innocent (i.e. legal). In other words, unless you can cite specific rules for why something you've written is legal Java code, it isn't. So here are some rules you can use to justify that something you've written is legal Java code.
An expression is generally interesting for its type and value; a statement is generally interesting for its effect.
expression
of type int
.
expression
s separated by
an infix binary operator (
+
,
-
,
*
,
/
,
<
,
>
,
etc.) is an
expression
. (The type depends on the types
of the expression
s and which operator it is.)
Examples: 3+4
,
5 - 2 > 6
, but not
3 +
.
(
expression
)
is an expression
.
(Same type.) Note that the parentheses must be properly nested and
matched.
Examples: (3)
,
(3-(2*5))
,
(((5)))
, but not
(3
,
()
,
)3(
, or
5(+)2
.
expression
preceded by a prefix
unary operator (+
, -
)
is an expression
of the same (numeric) type.
Examples: -3
,
-(5 + + 6)
, but not
5-
or
*5
.
"
marks is an
expression
of type String.
Examples: "What's your name?"
,
"We're number 2!"
,
""
, but not
"Hi there!
,
What's your name?
, or
"I said "Hi!" to Fred"
.
(Note on this last one: once Java sees a "
mark, it
looks for the next one to indicate the end of the String. In this case,
the next one is just before Hi!
, so Java thinks
"I said "
is a String, Hi!
is something else, and " to
Fred"
is another String. If you want to put
"
marks inside a String, you have to put a backslash
in front of them, e.g.
"I said \"Hi!\" to Fred"
.
expression
(of type double
).
(In fact, there's more to it: you can use exponential notation, as in
6.02e23
, which means 6.02 * 1023.)
expression
.
method-name
(
expr
,
expr
, ...
)
is an expression
.
The allowable method-names depend on the type of the first expression;
the number and types of expressions allowed inside the parentheses
depend on the method-name. For example, "Hi
there!"
is an expression of type String, so one can use
String methods on it, e.g.
"Hi there!".length()
,
"Hi there!".concat("I'm fine.")
,
"Hi there!".indexOf("th")
,
"Hi there!".substring(4,6)
, but not
"Hi there!".length
(no parentheses),
"Hi there!".length(7)
(the
length
method doesn't accept an int parameter), or
"Hi there!".fooBar()
(there is no
fooBar
method in the String class).
new
Classname
(
expr
,
expr
, ...
)
is an expression
whose type is whatever Classname
was used;
the number and types of expressions allowed inside the parentheses
depend on the definition of the constructor.
For example,
new Buggle()
is an expression of type Buggle,
because the Buggle class has a constructor with no parameters, and
new Point(3,5)
is an expression of type Point,
because the Point class has a constructor that takes two int parameters.
You can of course combine syntax rules E7 and E7.5: the
Point class has methods getX()
, getY()
,
toString()
, equals(Object)
, and
distance(Point)
, among others, so one could say
new Point(3,5).getX()
,
new Point(3,5).getY()
,
new Point(3,5).toString()
,
new Point(3,5).equals(new Point(7,1))
, or
new Point(3,5).distance(new Point(7,1))
, but not
new Point(3,5).indexOf("th")
because
the Point class has no indexOf
method.
expression
. (The type is whatever type the
variable-name was declared to be.)
variable-name
=
expression
expression
, as long as the
variable-name is already declared. (Type is whatever type the variable-name was declared to be.)
data-type variable-name
=
expression
;
data-type
may be "int", "boolean", "double", "String", or others we haven't seen yet.)
Effect: declares the variable, so that henceforth it stands for
that expression. If the type of the expression doesn't match the type
specified for the variable, it's an error.
data-type variable-name
;
expression
;
return
expression
;
return
statement won't happen.