Java

Java Section

Java is a Third Generation Object Oriented Programming language. This means that within the language several objects are defined and these are treated as such. Each object has its own responsibility. The Object Oriented principle is a way of thinking that cannot be explaned in one simple line. This way of thinking can only be achieved by practising a lot and reading many books and code. This page is not meant as a tutorial about the Java language. If you are looking for a tutorial, you might want to take a look at the links to Java Tutorials at the Java Links page.

Java is designed to be portable. Java code is compiled into bytecode (and not bit code) using javac. Bit code is machine code and is specific for a certain kind of hardware. Since Java code is compiled into byte code, an interpreter must be used to actually run the Java program. Byte code is not hardware dependent and this makes it possible to run a Java program on different platforms. A Java program can be executed on any platform for which there is a Java interpreter available. Many persons say about Java "Write once, run anywhere". Well this is only true for the platforms for which there is an interpreter, but nowadays for almost any (widely used) platform there is an interpreter available.

Java is also a Web language. Most Web browsers are equipped with a Java interpreter and are able to execute Java applets. Applets are small Java programs that can be used in websites. Because of security reasons the permissions of applets are very limited. For instance: they can only connect to the machine where they retrieved from (and where there codebase is). Since Java also is designed to work in an network environment such as the internet, Java treats URLs just as easily as most languages treat files.

Java is similar to C++, but:

  • No multiple inhertance in classes (but there is multiple inheritance in interfaces)
  • No pointers (but there are references)
  • You don't have to worry about deleting objects - Java has a build-in garbage collector!
  • No preprocessor
  • No structs or unions
  • No enums
  • No typedef
  • Java has packages

Hello World Example

In almost any programming language the first thing you learn is how to print the text "Hello World". This is also in Java the case. The Hello World program is one of the most basic and simple programs that can be written in the Java language. Example code for this program is listed below.

/**
 * This is just a sample program that writes a message to the screen.
 */
class HelloWorld {

   public static void main(String[] args) {
      System.out.println("Hello World!");
   }
}

This file should be saved in a file with the name HelloWorld.java so that it can be compiled to the file with the name HelloWorld.class. Most compilers will compile it if the program is saved in a file with another name, but the compiler will at least give a warning, as one of the conventions of Java is that each class is saved in a file with the same name as the class.

This class can be run, because it has a method (functions are called methods within Java) with the name "main". This method takes a String array as parameter. This array of string is used to pass arguments passed by the person that runs this program (the user) to the program. In this simple program the arguments passed by the user will be ignored, so these are irrelevant here. The first and only thing this main method does is a call to the System class to use its standard output stream and call the println method to write the "Hello World" string to the screen.

It is possible to change the simple program above in such a way that it makes use of the Object Oriented approach. An example of this modification is shown below.

/**
 * This is just a sample program that writes a message to the screen.
 * This program creates a simple object to write the message to the
 * screen. In this way only that object is responsible for that.
 */
class HelloWorld {

   // Constructor
   public HelloWorld() {
      System.out.println("Hello World!");
   }

   public static void main(String[] args) {
      HelloWorld helloWorld = new HelloWorld();
   }
}

This program also has a main method that makes it possible to run its code. When a class is executed the main method is called first. If a class doesn't contain a main method it is not possible to execute the code of that class on its own. Another class will be necessary to call that class.

When the class above is executed the main method is called. The first thing that the main method does is to create an object of the type HelloWorld. The object gets the name helloWorld. Note that Java is case sensitive. So HelloWorld is not the same as helloWorld. When the HelloWorld object is created a call is made to the constructor of that object.

The constructor is used to construct a new instance of the object. This constructor makes a call to the System class to use its standard output stream and call the println method to write the "Hello World" string to the screen. In Java you don't have to worry of destroying objects which are not used, because Java has its own built-in Garbage Collector to take care of that. That's why there is no code to destruct the object.

Program written by Peter

To run the program below your system has to be equipped with a Java engine. Most browsers have a built-in Java Virtual Machine, so in most cases it shouldn't be a problem to load the page linked below. Other browsers will prompt you with a warning that Java needs to be installed and a question if you want to do this. If you are having problems accessing the page below you might want to go to the plugin download page of the Java Sun site to get the Java plugin for your browser.

Links

Because the amount of Java related links on this site a separate Java Links page is available:

Contact the author: Send an electronic mail to: pajtroon@dds.nl.
Peter's ICQ Number is: #3900785.

This page: Copyright © 2002 Peter A. J. Troon

Note: This page is part of the Peter Troon Site.