-
Class declarations:
class-modifiers class class-identifier
[extends class-name]
[implements interface-name [, interface-name]*]
{
class body
}
- A file contains one class declaration, and is the same name as the public class.
- The class-modifiers are public, abstract, final, or none.
- The class-identifier is a name, by convention beginning with an upper-case letter.
- The super-class of this class is named in the optional extends clause.
A class extends java.lang.Object by default.
- Any interfaces the class implements are named in the optional implements clause.
- Class body include field declarations, method declarations,
and constructor declarations, among others. These have
class scope.
-
Field declarations:
field-modifiers type variable-declarator [, variable-declarator]* ;
- The field-modifiers include the visibility modifiers
public, protected, private; static for class variables;
final for constants, or none.
-
If a field is not static, then it is an instance variable.
-
If the field has no visibility modifier, than it has package scope.
-
A variable-declarator is of the form variable-identifier
or
variable-identifier = variable-intializer
Scalars are initialized by an expression. Arrays are initialized
using the syntax
{ expression, expression, ... }
-
Method declarations:
method-modifiers result-type method-identifier
( formal-parameters )
[throws exception-name [, exception-name]*]
{
method body
}
- The Method-modifiers include the visibility modifiers
public, protected, private; static for class methods; final
for methods which cannot be overriden; synchronized and
abstract; or none.
-
If a method is not static, then it is an instance method.
-
If the method has no visibility modifier, than it has package scope.
-
If the method is abstract, the method-body,including the braces,
is replace with a semi-colon.
-
The return-type is either a type or void.
-
The method-body is a series of statements,
including local-varaible-declarations.
-
Constructor declarations:
constructor-modifiers class-identifier ( formal-parameters )
[throws exception-name [, exception-name]*]
{
constructor body
}
- The constructor-modifiers include are the visibility modifiers
public, protected, private, or none.
-
The constructor-identifier must be the class name. The constructor has no return type,
not even void.
-
If the constructor has not visibility modifier, than it has package scope.
-
If no constructors are defined, a default, no argument constructor is automatically
provided.
-
Constructors are not inherited from the super-class.
-
The constructor-body is a series of statements,
including local-variable-declarations.
-
Statements
expression ;
if ( expression )
{
statments
}
[else {
statements
}]
while ( expression )
{
statements
}
do {
statements
} while ( expression ) ;
switch ( expression )
{
case constant : statments
[case constant : statments]*
[ default: statements ]
}
for ( local-variable-declarator or expression ;
expression ; expression )
{
statments
}
label : statment
break ;
continue ;
return expression ;
throw expression ;
synchronized ( expression )
{
statements
}
try {
statemnts
}
catch ( formal-parameter ) {
statements
}
[catch ( formal-parameter ) {
statements
} ]*
[finally {
statements
}]
-
The expressions in the if, while and do-while statements must evaluate to type boolean,
as must the middle expression in the for statement.
-
The expression in the switch statement must be of type char, byte, short or int.
-
The expression in the throw statement must be throwable.
-
The expression in the synchronized statement must be a reference type.
-
Local variable declarations:
type variable-identifier [= variable-initializer ]
[, variable-identifier [= variable-initializer]]*
;
-
Formal parameters:
type variable-identifier [, type variable-identifier]*
-
Type:
primitive-type or reference-type []*
-
The primitive types are: boolean,
the integer types byte, short, int, long, char,
and the floating-point types float, double.
-
Reference types is any type which is not primitive, including classes, interfaces,
and arrays.
-
Arrays are of array type, and can be cast to Object.
-
Expressions:
Literal
Identifier[.name]*
expression [ expression ]
expression( expression, expression ... )
( expression )
prefix-operation expression
expression postfix-operation
expression operation expression
( type ) expression
( expression ) ? expression : expression ;
this
new class-identifier ( expression, expression, ... )
new primitive-type [] []*
-
Literals include: integer (e.g. 1, 03, 0xff), floating-point (e.g.
1.0, 1.0e-3), boolean (true, false), character (e.g. 'a'),
string (e.g. "hi", and null.
-
Operations include assignment (e.g. =, +=, etc.);
short-circuit conditional; equality and relational (e.g. ==, !=, >);
bitwise logical; arithmetic including remainder (%);
unary pre-/post- increment/decrement; string concatentation using +;
shift operators.
-
The right-shift >>. sign-extends. The right-shift >>>. zero-fills. Two operations
are needed since all Java integral types are signed.
-
There are no derefernce (*) or address-of (&) operators in Java.
-
Packages:
package package-name ;
import fully-qualified-type-name ;
import package-name.* ;
-
The import of java.lang.* is automatic.
-
Import-on-demand syntax uses a * to import all public types in a package
-
These must be the first non-comment lines in the file.