Java Technology
Core Language Summary
("Cheat Sheets")

Java Syntax Overview

This overview of the Java environment programming language explains concepts necessary to understand the organization of the three page syntax summary that follows. These three pages may be used as set of "cheat sheets" for quick reference of syntax while writing Java programs.

Classes

The fundamental program unit of the Java environment programming language is an object-oriented construct called the "class." A class, which must be named, consists of declarations for data ("fields") and subroutines ("methods") that operate on the data fields.

In Java, fields and methods always exist in the context of a class, and never in isolation as in most previous programming languages.

 
    class ClassName          // class declaration
    {                        // start of class definition
      field declarations     // zero or more data fields
      method declarations    // zero or more methods
    }                        // end of class
 
(Note: A single-line comment in Java starts with //. Everything after the // to the end of the line is ignored by the compiler and other Java Development Kit tools.)

Blocks and Method Bodies

All executable code in the Java environment programming language occurs in a syntactical construct called a "block." Blocks begin with an open curly brace {, end with a closed curly brace }, and constain zero or more "statements." A method body, the code that comprises the executable part of a method, is a single block (but may have other blocks nested within it).
 
    class ClassName {        // class declaration
    {                        // start of class definition
      field declarations     // zero or more data fields
      method declaration 
      {                      // start of method body block
        statements           // zero or more statements
      }                      // end of method body block
    }                        // end of class
 

Abstract Methods and Classes

A method declaration may be followed by a semicolon ( ; ) instead of a method body block. The semicolon indicates that the definition for the method body is null (it will be supplied by another class). A method with a null body is incomplete, or "abstract," in the context of the class in which it is declared. A class that contains an abstract method is itself incomplete, so is called an "abstract class".
    abstract class ClassName // abstract class declaration
    {                        // start of class definition
      field declarations     // zero or more data fields
      method declaration
      {                      // start of method body block
        statements           // zero or more statements
      }                      // end of method body block

      abstract method decl ; // semicolon implies null body
    }                        // end of class

Interfaces

Imagine an abstract class in which all the methods are abstract, i.e., all method bodies are to be supplied by another class. Such a program unit is called an "interface." An interface contains only abstract method declarations; it should not contain field declarations or complete methods (methods with body blocks).
 
    interface InterfaceName  // interface declaration
    {                        // start of interface definition
      abstract method decl ; // zero or more abstract methods
    }                        // end of interface
 

Packages

A Java "package" is a collection of related classes and interfaces, which must be stored together, e.g., in the same directory. Thus, one package equates to one directory, and its classes and interfaces (one or more) should be stored one per file.
 
    package
      classes
      interfaces
 

Programs

A Java program consists of one or more packages. The package directories may be URLs, allowing a Java program to be network-distributed. (which is why we can load applets across the Web.)
 
    Java program
      packages
 

Syntax Summary Organization

The following syntax summary of the Java environment programming language consists of three sections:
  1. Syntax for class declarations
  2. Syntax for interface declarations
  3. Syntax for blocks (method bodies)

These three Syntax Summaries are not, by themselves, useful for learning the Java environment programming language. However, they are useful for quick reference once the basics of the language are understood.

Syntax Summary Usage

Use the Class Declaration Syntax Summary as a guide for writing a class as a whole. Then use the Method Body Syntax Summary as a guide for writing the statements for each method body block. Similarly, use the Interface Declaration Syntax Summary as a guide for writing interfaces (the methods of which have no body blocks).

The Summaries include all of the keywords and options that might possibly be used in a syntactical construct, including mutually exclusive syntax. One must understand the Java language to know the appropriate syntactical combinations - the Summaries do not differentiate. The basic premise of the Summaries is that it is easier to delete written syntax that is not needed than to remember needed syntax that is not yet written.

These three Syntax Summaries comprise the declaration and statement syntax for Java version 1.0.2 (JDK 1.0.2). Additional syntax for a construct called "inner classes," added to the language with JDK 1.1, is not included due to the controversial nature of inner classes.


Class Declaration Syntax Summary

package this_package_name;             // package declaration
import ext_package_name.ExtClassName;  // refer to class by its simple name
import ext_package_name.*;             // refer to all classes in pkg by simple name

                                       // Class Declaration Statement
public abstract final synchronized     // class declaration modifiers
  class ClassName                      // name of class
  extends SuperClassName               // name of superclass
  implements InterfaceName             // interfaces implemented by class

{                                      // start of class specification

                                       // Constant Field Variable Declaration
  private static final                 // constant declaration modifiers
    data_type_name FIELD_NAME          // data type and name of field
    = const_expr;                      // field initialization expression

                                       // Constant Field Array Declaration
  private static final                 // constant declaration modifiers
    data_type_name[] FIELD_NAME        // data type and name of field
    = {const_expr, ... };              // array initialization expression

                                       // Field Variable Declaration
  private transient volatile           // field declaration modifiers
    data_type_name field_name          // data type and name of field
    = const_expr;                      // field initialization expression

                                       // Field Array Declaration
  private transient volatile           // field declaration modifiers
    data_type_name[] field_name        // data type and name of field
    = {const_expr, ... };              // array initialization expression

  static {                             // Static Initializer
    statements;                        // see Method Body Syntax
  }                                    // end of static initializer

                                       // Constructor Definition
  public|protected|private ClassName   // visibility and name of constructor
    (data_type_name arg_name, ...)     // argument list declaration
    throws exception_name              // declaration of exceptions thrown
  {                                    // start of constructor body
    super (argument_list);             // call to superclass constructor
    this (argument_list);              // call to other local constructor
    statements;                        // see Method Body Syntax
  }                                    // end of constructor body

                                       // Method Definition
  public|protected|private             // method visibility modifiers
    static final synchronized native   // other method declaration modifiers
    data_type_name|void methodName     // method return type and name
    (data_type_name arg_name, ...)     // argument list declaration
    throws exception_name, ...         // declaration of exceptions thrown
  {                                    // start of method body
    statements;                        // see Method Body Syntax
  }                                    // end of method body

                                       // Abstract Method Declaration
  public|protected|private             // method visibility modifiers
    abstract                           // other method declaration modifiers
    data_type_name|void methodName     // method return type and name
    (data_type_name arg_name, ...)     // argument list declaration
    throws exception_name, ...         // declaration of exceptions thrown
    ;                                  // null (missing) method body

                                       // Main Method Definition
  public static void main              // main method modifiers and name
    (String[] args)                    // main method argument list
  {                                    // start of method body
    statements;                        // see Method Body Syntax
  }                                    // end of method body

}                                      // end of class specification

As a convenince, this Class Declaration Syntax Summary has been put into the form of a Class Coding Template.


Interface Declaration Syntax Summary

package this_package_name;             // package declaration
import ext_package_name.ExtClassName;  // refer to class by its simple name
import ext_package_name.*;             // refer to all classes in pkg by simple name

                                       // Interface Declaration Statement
public interface InterfaceName         // interface visibility and name
   extends SuperInterfaceName, ..      // name of superinterfaces

{                                      // start of interface specification

                                       // Abstract Method Declaration                                        
  public abstract                      // method declaration modifiers
  data_type_name|void methodName       // method return type and name
    (data_type_name arg_name, ...)     // argument list declaration
    throws exception_name, ...         // declaration of exceptions thrown
    ;                                  // null (missing) method body

}                                      // end of interface declaration

As a convenince, this Interface Declaration Syntax Summary has been put into the form of an Interface Coding Template.


Method Body Syntax Summary

                                   // Block
{                                  // start of block
  VariableDeclarations_opt         // block may contain var declarations
  Statements_opt                   // and statements, or may be empty
}                                  // end of block
    
                                   // Local Variable Declarations
type var_name;                     // creates uninitialized primitive var
Type var_name;                     // creates uninitialized object ref var
Type[] var_name;                   // creates array object ref variable
type var_name=init_value_expr;     // creates prim var with initial value
Type var_name=init_value_expr;     // creates obj ref var and obj with value
Type[] var_name = new Type[n];     // creates empty array of length n
Type[] var_name = {const,..const}; // creates array with initial values

                                   // Statement
Block                              // blocks can be nested
;                                  // empty statement, does nothing
expr;                              // expression is statement if ; added

                                   // Flow Control Statements
if (expr) Statement                // simple if statement, expr type boolean
if (expr) Statement else Statement // if-else stmt, 1st stmt not simple-if,
                                   // 2nd stmt executed if expr false

switch (expr) {                    // expr type char, byte, short, or int
  case expr_const:                 // multiple cases may share one stmt
  case expr_const: Statement       // expr_const assignable to switch expr
                   break;          // prevents falling thru to next case
  default: Statement               // default case optional, may be only one
}                                  // end of switch

while (expr) Statement             // expr type boolean, stmt executed 0 or
                                   // more times until expr false
do Statement while (expr);         // expr type boolean, stmt executed 1 or
                                   // more times until expr false

for (init_expr_opt;                // init expression executed first, once
     expr_opt;                     // expr type boolean, use break if none
     update_expr_opt) Statement    // expr-Statement-update_expr sequence
                                       // runs 0 or more times until expr false

identifier: Statement              // ident label used with break,continue
                                   // Statement must be while, do, for

break;                             // xfer to enclosing switch,while,do,for
                                   // and complete stmt normally
break identifier_opt;              // xfer to labeled while, do, or for and
                                   // complete stmt normally

continue identifier_opt;           // xfer to enclosing (or labeled) while,
                                   // do, or for and begin next iteration

return;                            // xfer to caller (constructor,void meth)
return expr;                       // xfer to caller of method that returns

synchronized (ref_expr) Block      // lock object referenced by expr before
                                   // executing Block

                                   // Exception Handling Statement
try Block                          // 1st matching catch executed if Block
                                   // throws exception, exception xfers to
                                   // caller if no catch block matches
  catch (Type identifier) Block    // Type is subtype of Throwable, must
    :                              // match exceptions thrown by try block
  catch (Type identifier) Block    // catch clause optional
  finally Block                    // always executed unless System.exit(n)
                                   // executed in try or catch Blocks, opt
 
throw expr;                        // xfer to try stmt that catches expr value


Note on Inner Classes

An e-mail discussion among Java instructors (in which L participated) revealed that most of the instructors believed inner classes should be used sparingly. L firmly believes mere mortals should avoid them entirely.


[home]
[OO] [Java] [memorial ships]
[humor] [income tax repeal]


Copyright (C) 1998 LDJ Trust