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.

Outline


Expressions
An expression can be any of the following:
  1. a number, e.g. 738 or -3.52e-16
  2. a quoted string, e.g. "hello there!"
  3. a 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
  4. expression infix-operator expression
    where an infix-operator is something like +, -, *, /, %, <, >, <=, >=, ==, etc.

    For example, 3+4 is an expression, because

    1. 3 is an expression by rule 1 above, and
    2. + is one of the predefined infix operators, and
    3. 4 is an expression by rule 1 above, so
    4. 3+4 is an expression by rule 4

    However, 3+ is not an expression:

    1. 3 is an expression by rule 1, and
    2. + is one of the built-in infix operators, but
    3. there is no rule saying that an expression followed by an infix operator is an expression.

  5. (expression)

    For example, (x+4)*5 is an expression, because

    1. x is an expression by rule 1 (as long as x is appropriately declared),
    2. + is one of the built-in infix operators,
    3. 4 is an expression by rule 1,
    4. x is an expression by rule 4,
    5. (x+4) is an expression by rule 5,
    6. 5 is an expression by rule 1, and
    7. * is one of the built-in infix operators, so
    8. (x+4)*5 is an expression by rule 4

  6. expression . variable
    (only makes sense if the expression is of a type that has an instance variable of the specified name)

    For example, ...

  7. expression . method-name ()
    expression . method-name ( expression )
    expression . method-name ( expression , expression )
    etc.
    (only makes sense if the expression is of a type that has a method of the specified name and number of parameters)

    For example, ...

  8. variable = expression

    For example, ...

  9. new class-name ()
    new class-name ( expression )
    new class-name ( expression , expression )
    etc. If there are expressions in between the parentheses, it only makes sense if the class has a constructor with a matching number of type of parameters.

    For example, ...

Variable declarations
data-type variable-name ;
and
data-type variable-name = expression ;
are both variable declarations. They both create a new variable of the specified variable name; the latter initializes the variable to stand for the value of the specified expression, while the former gives the variable a "default value", which depends on the data type. Most compilers will complain if you try to use a variable that hasn't been initialized. Note that a variable can only hold the type of data it was declared to hold; you can't declare an int variable and then later put a String into it.
Statements
Defining a Method
Classes