Java:Tutorials:Basics

From GPWiki
Jump to: navigation, search

Java Basics

A first Java program

I will attempt to explain the basics of Java using the traditional example of the "Hello World!" program.

The code is as follows:

// HelloWorld - A traditional HelloWorld program in Java
class HelloWorld {
  public static void main(String[] args) {
    System.out.println("Hello World!");
  }
}

The program should be saved in a text file called "HelloWorld.java", "HelloWorld" to indicate the main class, and ".java" to indicate it's a Java source file. You should be able to compile and run this from your IDE, else you can compile it by typing "javac HelloWorld.java" at the command line. This should produce a file called "HelloWorld.class", which you can execute by typing "java HelloWorld".

I will now go through each part of the program bit by bit. If I type ..., it is to indicate that I have left part of the program out whilst explaining this part.

// HelloWorld - A traditional HelloWorld program in Java

This is a comment. Comments are in typical C/C++ format, meaning any line that begins with "//" is treated as a comment and will be ignored by the compiler. Multilined comments can be made by enclosing the comment between "/*" and "*/". The purpose of comments are to document the code to make it easier for other programmers to read and understand.

class HelloWorld {
  ...
}

This is a class definition. As Java is an object orientated language language, any functions we have must be encapsulated as part of a class. A class is a blueprint for making an object, which we will see later. The keyword "class" indicates that we are defining a new class, and "HelloWorld" is the name of the class. The class body comes between the curly braces ("{" & "}").

public static void main(String[] args) {
  ...
}

This is a method header. Methods, also known as functions, are blocks of code that are exectued, that is they are the actual algorithm. The method header tells the compiler a few important things about the method you are writing. The first word, "public", is a visibility modifier. A public function can be called by anyone. The second word, "static", is of particular notice. A static function can be called directly from a class without having to create an object of a class. What this means will become more clear as you learn more Java, but for now, know that the main method must be static. All functions have the opportunity to return a value, the word "void" simply means it does not. Finally, we get to the name of the method, in this case, "main". The "main" method is special, because it will be called by the system when the program is first run. In between the brackets, we have the functions arguments. These are values that the caller of the function can pass to the function to work with. In this case, we have an array of Strings called args. This is a place where the system puts any arguments passed to the program on the command line. The actual method body comes between the curly braces.

The exact details of this method header will probably not become clear to a beginner programmer straight away. Just remember that you need to have this header for your main method in every program.

System.out.println("Hello World!");

Finally we actually get to the code that actually does what we want. In this case, this line uses the System.out.println() method to print "Hello World" to the screen, followed by a new line. A semicolon (";") must be placed after every executable statement in java.


There you have it! A very basic program in java.

Troubleshooting

Now you've got your program ready, but can't remember how to make it run! No fear, I've already anticipated this:

  1. Have you got the Java Development Kit installed on this computer?
    • You'll have a hard time without it! Go to the Java Website and download it.
  2. You might also need to setup some paths.
    • Go into your autoexec.bat, and at the bottom type these:
      • SET PATH=%PATH%;C:\Program Files\Java\jdk1.5.0\bin
      • SET CLASSPATH=%PATH%
  3. Go to the command prompt and type the following:
    • javac C:\path\name\here\TestProgram.java (Make sure it's the same name as the class name!)
  4. Barring any errors or warnings, it will say nothing. Then type:
    • java TestProgram (Do not include an extension!)
  5. Bask in the glory of your creation.

If there are errors when you execute it, you'll want to do some debugging. Have fun with that!


Closing Thoughts

Going back to Java can sometimes be hard. You don't remember how to do things, or didn't know how to begin with. Fortunately, those guys over at Sun have done a lot of documenting, and have posted it on the web:

JDK 5 Documentation

It's also available for download here:

JDK 5 Documentation Download