A Few Grammar Rules of Java

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.

Expressions and statements

An expression is generally interesting for its type and value; a statement is generally interesting for its effect.

Expressions

E1: integer literal
A sequence of one or more digits is an expression of type int.
E2: infix binary operation
Two expressions separated by an infix binary operator ( +, -, *, /, <, >, etc.) is an expression. (The type depends on the types of the expressions and which operator it is.)

Examples: 3+4,
5 - 2 > 6, but not
3 +.

E3: parentheses
( 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.

E4: prefix unary operation
An expression preceded by a prefix unary operator (+, -) is an expression of the same (numeric) type.

Examples: -3,
-(5 + + 6), but not
5- or
*5.

E5: String literal
A sequence of numbers, letters, spaces, and punctuation surrounded by " 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".

E6: double literal
A sequence of digits followed by a period and another sequence of digits is an 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.)

E7: method call

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).

E7.5: constructor call

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.

E8: variable name
Any previously-declared variable-name is an expression. (The type is whatever type the variable-name was declared to be.)
E9: assignment
variable-name = expression
is an expression, as long as the variable-name is already declared. (Type is whatever type the variable-name was declared to be.)

Statements

S1: variable-declaration statement
data-type variable-name = expression ;
is a statement. (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.
You can declare more than one variable in one statement, separated by commas.
S1.5: variable-declaration without initialization
data-type variable-name ;
is a statement. Effect: declares the variable to hold the specified type, but doesn't specify an initial value. For local variables, if the variable is used before being given a value, you'll get an error message; for instance variables, it'll be given a default value (0 for ints, 0.0 for doubles, null for Objects). As above, you can declare more than one variable in one statement, separated by commas.
S2: expression statement
expression ;
is a statement. Effect: finds the value of the expression and ignores it.
S3: return statement
return expression ;
is a statement. Effect: finds the value of the expression and returns it as the value of the current method. Any statements after the return statement won't happen.