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.
expression
can be any of the following:
number
, e.g.
738
or -3.52e-16
quoted
string
, e.g.
"hello there!"
variable
name
(only legal if the variable is declared somewhere with
a scope including the part of the code where we are now), e.g.
x
or myCSProfessor
expression
infix-operator
expression
infix-operator
is something like
+, -, *, /, %, <, >, <=, >=, ==
, etc.
For example, 3+4
is an expression
, because
3
is an expression
by rule 1 above, and +
is one of the predefined infix operators
, and 4
is an expression
by rule 1 above, so 3+4
is an expression
by rule 4
However, 3+
is not an
expression
:
3
is an expression
by rule 1, and+
is one of the built-in
infix operators
, butexpression
followed by an
infix operator
is an
expression
.(expression
)
For example, (x+4)*5
is an expression
, because
x
is an expression
by rule 1 (as long as
x
is appropriately declared),+
is one of the built-in infix operators
,4
is an expression
by rule 1,x
is an expression
by rule 4,(x+4)
is an expression
by rule 5,5
is an expression
by rule 1, and*
is one of the built-in infix operators
, so(x+4)*5
is an expression
by rule 4expression
.
variable
expression
is of a type that has an instance variable of the specified name)
For example, ...
expression
.
method-name
()
expression
.
method-name
(
expression
)
expression
.
method-name
(
expression
,
expression
)
expression
is of a type that has a method of the specified name and number of
parameters)
For example, ...
variable
=
expression
For example, ...
new class-name
()
new class-name
(
expression
)
new class-name
(
expression
,
expression
)
For example, ...
data-type
variable-name
;
data-type
variable-name
=
expression
;
int
variable and then later put a String
into
it.