Using objectdrawTopHello World!

Hello World!

I have now added lots of material on Java to the documentation and handouts page on the course web pages. In particular you can find a free Java test as well as complete documentation on the Java version of the objectdraw library.

We will be running programs inside the (free) Eclipse development environment, which is installed on all of our lab computers. On the documentation page you can find instructions on how to install eclipse on your own computer if you would like. We demonstrated how to load a program in Eclipse.

The first program in any new language is by tradition one that prints "Hello World".

In Grace we wrote this as

   print "Hello World!"

There is more overhead in Java, as all Java code must be enclosed in a class:

public class HelloJava {

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

The name of the class is HelloJava. It has a single method main. which is run automatically when the program is started with this class. Inside the method is a single print statement that prints out "Hello World!"

Let's take this a line at a time. The first line of your program (after any imports) is generally a class header. Classes in Java, unlike in Grace, do not take parameters. Initialization is general done through separate constructor code, or, if we are writing code using the objectdraw library, using a special begin method. In this case we have no instance variables, and hence, nothing to initialize, so we get to skip that.

All identifiers introduced in Java need to have a declared visibility. Classes are virtually always declared to be public, as in this case. The body of the class is contained inside "{" and "}". This trivial class has only the main method. If you start executing a class, the system looks for a main method with exactly the same declaration as shown. All the magic words are required. It is public, of course. A method is static if it is associated with the class rather than instances of the class. That means you can execute it without having created an object from the class. The String[] args parameter is something that we will not generally use, but it allows us to get values from the environment if we desire.

The println method is in the library named System. It has a (public) object named out that can print strings in the output area at the bottom of the Eclipse window. System.out.println will print the parameter and then move to the beginning of the next line, so that any succeeding prints will show up on that next line. On the other hand, System.out.print will display the value of the parameter, but not move to a new line. Hence the next print will display on the same line.


Using objectdrawTopHello World!