Java Programming/Understanding a Java Program

This article presents a small Java program which can be run from the console. It computes the distance between two points on a plane. You do not need to understand the structure and meaning of the program just yet; we will get to that soon. Also, because the program is intended as a simple introduction, it has some room for improvement, and later in the module we will show some of these improvements. But let's not get too far ahead of ourselves!

The Distance Class: Intent, Source, and Use
This class is named Distance, so using your favorite editor or Java IDE, first create a file named, then copy the source below, paste it into the file and save the file.

At this point, you may wish to review the source to see how much you might be able to understand. While perhaps not being the most literate of programming languages, someone with understanding of other procedural languages such as C, or other object oriented languages such as C++ or C#, will be able to understand most if not all of the sample program.

Once you save the file, compile the program:

(If the  command fails, review the installation instructions.)

To run the program, you supply it with the x and y coordinates of two points on a plane separated by a space. For this version of Distance, only integer points are supported. The command sequence is   to compute the distance between the points (x0, y0) and (x1, y1).

Here are two examples:

We'll explain this strange looking output, and also show how to improve it, later.

Detailed Program Structure and Overview
As promised, we will now provide a detailed description of this Java program. We will discuss the syntax and structure of the program and the meaning of that structure.

Introduction to Java Syntax

 * Figure 2.1 : Basic Java syntax.
 * For a further treatment of the syntax elements of Java, see also Syntax.

The syntax of a Java class is the characters, symbols and their structure used to code the class. Java programs consist of a sequence of tokens. There are different kinds of tokens. For example, there are word tokens such as and  which represent  keywords  (in purple  above  ) &mdash; special words with reserved meaning in Java. Other words such as,  ,  , and   are not keywords but identifiers (in grey). Identifiers have many different uses in Java but primarily they are used as names. Java also has tokens to represent numbers, such as  and  ; these are known as  literals  (in orange).  String literals  (in blue), such as  , consist of zero or more characters embedded in double quotes, and  operators  (in red) such as   and   are used to express basic computation such as addition or String concatenation or assignment. There are also left and right braces ( and  ) which enclose blocks. The body of a class is one such block. Some tokens are punctuation, such as periods  and commas   and semicolons. You use whitespace such as spaces, tabs, and newlines, to separate tokens. For example, whitespace is required between keywords and identifiers:  is a single identifier with twelve characters, not two Java keywords.

Declarations and Definitions

 * Figure 2.2 : Declarations and Definitions.

Sequences of tokens are used to construct the next building blocks of Java classes as shown above: declarations and definitions. A class declaration provides the name and visibility of a class. In our example,  is the class declaration. It consists (in this case) of two keywords,  and   followed by the identifier.

This means that we are defining a class named. Other classes, or in our case, the command line, can refer to the class by this name. The  keyword is an access modifier which declares that this class and its members may be accessed from other classes. The  keyword, obviously, identifies this declaration as a class. Java also allows declarations of interfaces and annotations.

The class declaration is then followed by a block (surrounded by curly braces) which provides the class's definition (in blue in figure 2.2). The definition is the implementation of the class – the declaration and definitions of the class's members. This class contains exactly six members, which we will explain in turn.
 * 1) Two field declarations, named   and   (in green)
 * 2) A constructor declaration (in orange)
 * 3) Three method declarations (in red)

Example: Instance Fields
The declaration

...declares two instance fields. Instance fields represent named values that are allocated whenever an instance of the class is constructed. When a Java program creates a  instance, that instance will contain space for   and. When another  object is created, it will contain space for its own   and   values. The value of  in the first   object can vary independently of the value of   in the second   object.

This declaration consists of:


 * 1) The  access modifier, which means these instance fields are not visible to other classes.
 * 2) The type of the instance fields. In this case, the type is . This is the class   in the   package.
 * 3) The names of the instance fields in a comma separated list.

These two fields could also have been declared with two separate but more verbose declarations,

Since the type of these fields is a reference type (i.e. a field that refers to or can hold a reference to an object value), Java will implicitly initialize the values of  and   to null when a   instance is created. The null value means that a reference value does not refer to an object. The special Java literal is used to represent the null value in a program. While you can explicitly assign null values in a declaration, as in

It is not necessary and most programmers omit such default assignments.

Example: Constructor
A constructor is a special method in a class which is used to construct an instance of the class. The constructor can perform initialization for the object, beyond that which the Java VM does automatically. For example, Java will automatically initialize the fields  and   to null.

The constructor above consists of five parts:
 * 1) The optional access modifier(s). In this case, the constructor is declared
 * 2) The constructor name, which must match the class name exactly:   in this case.
 * 3) The constructor parameters. The parameter list is required. Even if a constructor does not have any parameters, you must specify the empty list  . The parameter list declares the type and name of each of the method's parameters.
 * 4) An optional  clause which declares the exceptions that the constructor may throw. This constructor does not declare any exceptions.
 * 5) The constructor body, which is a Java block (enclosed in  ).  This constructor's body contains two statements.

This constructor accepts four parameters, named  and. Each parameter requires a parameter type declaration, which in this example is  for all four parameters. The parameters in the parameter list are separated by commas.

The two assignments in this constructor use Java's  operator to allocate two  objects. The first allocates an object representing the first point,, and assigns it to the   instance variable (replacing the null value that the instance variable was initialized to). The second statement allocates a second  instance with   and assigns it to the   instance variable.

This is the constructor for the Distance class. Distance implicitly extends from. Java inserts a call to the super constructor as the first executable statement of the constructor if there is not one explicitly coded. The above constructor body is equivalent to the following body with the explicit super constructor call:

While it is true that this class could be implemented in other ways, such as simply storing the coordinates of the two points and computing the distance as $$\sqrt{(x_1-x_0)^2 + (y_1-y_0)^2}$$, this class instead uses the existing  class. This choice matches the abstract definition of this class: to print the distance between two points on the plane. We take advantage of existing behavior already implemented in the Java platform rather than implementing it again. We will see later how to make the program more flexible without adding much complexity, because we choose to use object abstractions here. However, the key point is that this class uses information hiding. That is, how the class stores its state or how it computes the distance is hidden. We can change this implementation without altering how clients use and invoke the class.

Example: Methods
Methods are the third and most important type of class member. This class contains three methods in which the behavior of the  class is defined: ,  , and

The printDistance method
The  method prints the distance between the two points to the standard output (normally the console).

This instance method executes within the context of an implicit  object. The instance field references,  and , refer to instance fields of that implicit object. You can also use the special variable  to explicitly reference the current object. Within an instance method, Java binds the name  to the object on which the method is executing, and the type of   is that of the current class. The body of the  method could also be coded as

to make the instance field references more explicit.

This method both computes the distance and prints it in one statement. The distance is computed with ;   is an instance method of the   class (of which   and   are instances). The method operates on  (binding   to the object that   refers to during the execution of the method) and accepting another Point as a parameter. Actually, it is slightly more complicated than that, but we'll explain later. The result of the  method is a double precision floating point number.

This method uses the syntax

to construct a String to pass to the. This expression is a series of String concatenation methods which concatenates Strings or the String representation of primitive types (such as doubles) or objects, and returns a long string. For example, the result of this expression for the points (0,3) and (4,0) is the String

which the method then prints to.

In order to print, we invoke the. This is an instance method from, which is the type of the static field   in the class. The Java VM binds  to the standard output stream when it starts a program.

The main method
The  method is the main entry point which Java invokes when you start a Java program from the command line. The command

instructs Java to locate the Distance class, put the four command line arguments into an array of String values, then pass those arguments to the  method of the class. We will introduce arrays shortly. Any Java class that you want to invoke from the command line or desktop shortcut must have a main method with this signature or the following signature:.

The  method invokes the final method, , four times. The  takes a single string parameter and returns the integer value represented in the string. For example,  will return the integer 3.

People who do test-first programming or perform regression testing write a main method in every Java class, and a main function in every Python module, to run automated tests. When a person executes the file directly, the main method executes and runs the automated tests for that file. When a person executes some other Java file that in turn imports many other Java classes, only one main method is executed -- the main method of the directly-executed file.

The method
The  method delegates its job to the   method. The main method could have called  directly; the   method simply makes the   method slightly more readable.

This method is since, like the fields   and , it is part of the internal implementation of the class and is not part of the external programming interface of the   class.

Static vs. Instance Methods
Both the  and   methods are static methods. The keyword tells the compiler to create a single memory space associated with the class. Each individual object instantiated has its own private state variables and methods but use the same static methods and members common to the single class object created by the compiler when the first class object is instantiated or created. This means that the method executes in a static or non-object context &mdash; there is no implicit separate instance available when the static methods run from various objects, and the special variable  is not available. As such, static methods cannot access instance methods or instance fields (such as ) or  ) directly. The   method can only invoke the instance method   method via an instance reference such as.

Data Types
Most declarations have a data type. Java has several categories of data types: reference types, primitive types, array types, and a special type, void.

Primitive Types
The primitive types are used to represent boolean, character, and numeric values. This program uses only one primitive type explicitly,, which represents 32 bit signed integer values. The program also implicitly uses, which is the return type of the   method of. values are 64 bit IEEE floating point values. The  method uses integer values 0, 1, 2, and 3 to access elements of the command line arguments. The  constructor's four parameters also have the type. Also, the  method has a return type of. This means a call to that method, such as, is an expression of type. This helps explain why the main method cannot call:

Since the type of the  array element is String, and our constructor's parameters must be , such a call would result in an error because Java will not automatically convert values of type String into   values.

Java's primitive types are ,, , , , , and . Each of which are also Java language keywords.

Reference Types
In addition to primitive types, Java supports reference type. A reference type is a Java data type which is defined by a Java class or interface. Reference types derive this name because such values refer to an object or contain a reference to an object. The idea is similar to pointers in other languages like C.

Java represents sequences of character data, or String, with the reference type  which is most commonly referred to as. String literals, such as   are constants whose type is String.

This program uses three separate reference types:
 * 1) java.lang.String (or simply String)
 * 2) Distance
 * 3) java.awt.Point


 * For more information see chapter: Java Programming/Classes, Objects and Types.

Array Types
Java supports arrays, which are aggregate types which have a fixed element type (which can be any Java type) and an integral size. This program uses only one array,. This indicates that  has an array type and that the element type is. The Java VM constructs and initializes the array that is passed to the  method. See arrays for more details on how to create arrays and access their size.

The elements of arrays are accessed with integer indices. The first element of an array is always element 0. This program accesses the first four elements of the  array explicitly with the indices 0, 1, 2, and 3. This program does not perform any input validation, such as verifying that the user passed at least four arguments to the program. We will fix that later.

void
is not a type in Java; it represents the absence of a type. Methods which do not return values are declared as void methods.

This class defines two void methods:

Whitespace
Whitespace in Java is used to separate the tokens in a Java source file. Whitespace is required in some places, such as between access modifiers, type names and Identifiers, and is used to improve readability elsewhere.

Wherever whitespace is required in Java, one or more whitespace characters may be used. Wherever whitespace is optional in Java, zero or more whitespace characters may be used.

Java whitespace consists of the
 * space character (0x20),
 * the tab character (hex 0x09),
 * the form feed character (hex 0x0c),
 * the line separators characters newline (hex 0x0a) or carriage return (hex 0x0d) characters.

Line separators are special whitespace characters in that they also terminate line comments, whereas normal whitespace does not.

Other Unicode space characters, including vertical tab, are not allowed as whitespace in Java.

Required Whitespace
Look at the method  :

Whitespace is required between and, between  and , between  and  , and between   and.

If the code is written like this:

...it means something completely different: it declares a method which has the return type  It is unlikely that this type exists and the method is no longer static, so the above would result in a semantic error.

Indentation
Java ignores all whitespace in front of a statement. As this, these two code snippets are identical for the compiler:

However, the first one's style (with whitespace) is preferred, as the readability is higher. The method body is easier to distinguish from the head, even at a higher reading speed.