A first step in Java

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.

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

ax^2 + bx + c = 0

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

A class can be defined in the following way:

     public class NAME {

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

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.

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.

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 */

A field is described by


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.

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

A method is defined by


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).

The simplest command is the assignment

        NAME = VALUE;

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!"

One can combine values with operators to get new values like in


Usual arithmetics:

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

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

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.

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

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.

The syntax


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.

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

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

QuadraticEquation qe1 = new QuadraticEquation(2.0, 10.0, 12.0);

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



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:

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

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


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.

We postpone the explanation of the argument args for the moment.

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


To run our first example, we call

    javac TestQuadraticEquation.java
    java TestQuadraticEquation

    2.0*x^2 + 10.0*x + 12.0

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.

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