COMP 212 -Summary of Java Syntax

General:

A Java program consists of one or more classes one of them must be public and must have a method with the following signature:

public static void main (String[] args).

Basically, the main() method will instantiate appropriate objects and send them "messages" (by calling their methods) to perform the desired tasks.  The main() method should not contain any complicated program logic nor program flow control.

 

Comments syntax:

// Line-oriented - comment goes to end of the current line.

 

/*

block-oriented

can span several lines.

*/

Class definition syntax:

[…] means optional.

[public] class class-name [inheritance-specification] {

[field-list;]

[constructor-list;]

[method-list;]

}

Example:

public class PizzaClient {

    /**
    * Compares the price/area ratio of the two parameters p1, and p2.
    * Returns true if p1 is a better deal than p2, false otherwise.
  */
  public boolean betterDeal(Pizza p1, Pizza p2) {

   
     return p1.getPrice()/p1.getArea() < p2.getPrice()/p2.getArea();
            // NOTE: infix notation for arithmetic/logical expressions,
   
         // and "dot" notation for method calls.

   
}

  /**
  * Instantiates a round pizza and a rectangular pizza and compare deals.
  */
   
public void run() {
     // Instantiates a Circle and a Pizza with this Circle as its shape,
     // and assign it to a Pizza variable called round:
     Pizza round = new Pizza (3.99, new Circle (2.5));

     // Instantiates a Rectangle and a Pizza with this Rectangle as its shape,
     // and assign it to a Pizza variable called rect:
     Pizza rect = new Pizza (4.99, new Rectangle (6, 4));

     // Calls System.out to print the result to the standard output.
     System.out.println(round + " is a better deal than " + rect + ": " +
                        betterDeal(round, rect));
    }

  /**
  * Main entry to the program to find the better deal.
  * Instantiates an instance of PizzaClient and tells it to run.
  * This is what all main() should do: instantiates a bunch of objects and
  * "turn them loose"!
  * There should no complicated logic and/or control in main().
  * @param nu not used
  */
  public static void main (String[] nu) {
     new PizzaClient().run();
   
}
}

Java Statement syntax:

NOTE: Each Java statement must terminate with a semi-colon.

public class Rectangle extends AShape {

private double _height; // Note the underscore.

private double _width;

public Rectangle (double width, double height) {

_height = height;

_width = width;

// the underscore helps distinguish the field from the parameter.

}

public double getArea() {

return _height * _width; // infix notation!

}

public String toString () {

return "Rectangle(width = " + _width + ", heigth = " + _height + ")";

}

}

Notes on the toString() method: toString() is a method that is inherited all the way from the base class, Object. It is the method that the Java system calls by default whenever a string representation of the class is needed. For instance, "This is "+ myObject is equivalent to "This is " + myObject.toString(). DrJava will call an object's toString() method if you type the object's name in the interaction window, without terminating the line with a semicolon. The return value of toString() is what prints out on the next line.

 

Field list syntax:

A field list consists of zero or more field declarations of the form

[static] [final] [public | private | protected] field-type field-name [assignment];

 

Constructor list syntax:

A constructor list consists of zero or more constructor definitions of the form

[public | private | protected]

class-name ([parameter-list]){

[statement-list;]

}

NOTE: The constructor's name is the same as the class name. Constructors are used for initialization of the object during the object's instantiation only.

Method list syntax:

A method list consists of zero or more method definitions of the form

[static] [final] [public | private | protected]

return-type method-name [param-list] {

[statement-list;]

}

A return type void means the method does not return any value.

param-list looks like:

type1 param1, type2 param2, …, typeN paramN