A first step in Java

$\triangleright$
Java is a very popular object oriented language, partly due to its clear design, partly because it is used as the general programming language of the web.

$\diamond$
For our first Java program we will construct a class QuadraticEquation, which describes an equation of the form

\begin{displaymath}
ax^2 + bx + c = 0
\end{displaymath}

Its fields are the three parameters a, b, c. An object of this class is given by special values for a, b, and c.

It has a method to create a new object with given parameters and a method to print itself on the screen.

Now we will go through the complete code of the class: QuadraticEquation.java

$\bullet$
A class can be defined in the following way:

     public class NAME {
         FIELD1;
         FIELD2;
         ..
         METHOD1  
         METHOD2
      }
 

$\triangleright$
On can write non-public classes that are only used for internal purposes of e.g. another class.

$\bullet$
A Java program can be broken up in lines arbitrarily (as long as one does not cut a word). Any amount of space is allowed between the words.

$\triangleright$
Usually one indents the lines in a systematic way to emphasize the structure of the program. This is only an aid for the human reader.

$\bullet$
A Java program can contain comments, i.e. texts that are meant only as explanation for the reader, not as a part of the program proper. Two forms of comments are:


     // any text up to the end of a line

     /* any enclosed text
        even spanning lines */
 

$\bullet$
A field is described by

TYPE FIELDNAME;

The type describes, what kinds of values can be used for a field, e.g. floating numbers, integer numbers or text strings. The name is used to reference the field in the program.

$\diamond$
Some important types are

Type Values
double floating point numbers with high precision (about 16 digits)
integer standard integer numbers (smaller than about 2 billions)
String small texts

$\bullet$
A method is defined by

   RETURNTYPE METHODNAME(ARGUMENTS) {
     COMMAND1;
     COMMAND2;
       ..
     COMMANDn;
   }
 

$\bullet$
A special kind of methods are those that create a new object of the given class, the constructors. They have the name of the class and no return type (not even void).

$\bullet$
The simplest command is the assignment

        NAME = VALUE;
 

$\diamond$
Constant values can have different forms, according to their type:

Type some constants
integer 42, 0, -123
double 3.141, -0.23, 1.23e5
String "Hello world", "This is not a String!"

$\bullet$
One can combine values with operators to get new values like in

     NAME = VALUE OP VALUE;
 

$\diamond$
Usual arithmetics:

     a = a1 + 3.0;
     b = a / 24.1;
 

     String s;
     s = "Hello " + "World";
     // results in s = "Hello World"
 

$\triangleright$
A very special operator in Java is the + between strings and anything else. It first creates a string from the other object, then combines the strings.

$\diamond$
double d;
d = 42.0;
s = "Number " + d;
// result: s = "Number 42.0"

$\triangleright$
Java comes with a large collection of predefined classes for many purposes (e.g. input/output, graphics, network connections). A special class is the class System, which contains a predefined object, which allows output to the screen. One of the methods of out is

println(String s)

which prints the string s to the screen.

$\triangleright$
The syntax

System.out.println(s)

means (leaving out some more complicated details): Get the predefined object out of class System and call its method println, passing the string s as parameter.

$\diamond$
Next we will look at the simple test program TestQuadraticEquation that uses our class QuadraticEquation: TestQuadraticEquation.java

$\bullet$
Before one can use an object of any class, one has to create it, using one of the constructors and the keyword new.

$\diamond$
QuadraticEquation qe1 = new QuadraticEquation(2.0, 10.0, 12.0);

$\bullet$
If one has an object of a given class, one can reach its fields or methods as

      OBJECT.FIELDNAME
      OBJECT.METHODNAME(ARGUMENTS)
 

$\diamond$
qe1.print();

$\triangleright$
Now we have a kind of ``chicken and egg'' problem: If we have an object, we can call its methods. If we are in a method, we can create objects. But how do we start? The solution comes in two parts:

$\bullet$
Methods that are denoted with the keyword static, can be called without an object, by using the class name instead of an object name.

$\diamond$
The standard Java class Double has a static method parseDouble(String s) to convert a String that describes a number, into the corresponding double value. It can be called as

     Double.parseDouble(s);
 

$\bullet$
To start a Java program one uses a class that contains a static method

public static void main (String[] args)

This method is executed, creates the needed objects and calls the necessary methods to get everything running.

$\triangleright$
We postpone the explanation of the argument args for the moment.

$\triangleright$
The exact procedure to start a Java program is:

  1. Create the program as a bunch of source files, one for each class (mostly), which have the name MYCLASS.java, MYCLASS being the name of the class defined in the file. One of the classes (at least) contains a main method.

  2. Compile everything by calling

    javac STARTCLASS.java

    The compiler javac decides itself, which additional classes are needed, and compiles them automatically. The result are files CLASSNAME.class for each needed class.

  3. Start the program with

    java STARTCLASS

$\diamond$
To run our first example, we call

    javac TestQuadraticEquation.java
    java TestQuadraticEquation

    2.0*x^2 + 10.0*x + 12.0

$\triangleright$
The Java compiler is special in that it doesn't produce a program in machine language, but in a universal ``Java byte code''. The program java (called the Java Virtual Machine or JVM) interprets this byte code and issues corresponding machine instructions.

$\triangleright$
This two-step procedure allows to compile a Java program once and run it on any kind of computer - if it has a JVM. One can look upon the JVM as a program simulating a theoretical computer that understands the byte code as its machine language.

previous    contents     next

Peter Junglas 8.3.2000