Your First Java Program

Now that you know something about what Java is and where it came from, how about looking at your first
Java application? The first program is, of course, the traditional “Hello World” program. Again, the code
below can be created with any text editor.


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


This code is saved in a file called “HelloWorld.java”. All code belongs in a class in Java. Line one of the code
above is the class declaration. The keyword “public” makes this class accessible outside of the file. It also
forces the file to be named “HelloWorld.java”. Because the files and public classes must have the same name
in Java, you create a new file for every public class.

“main” is a method in this Java class. Think of methods as actions or operations the program can do. The
keyword “public” appears again in front of the main method.  Public makes this method available outside of
this class as well so other code can access it. Notice the word “static” right after “public” in the main method.
You will look at static in more detail in a later chapter. For now make sure it’s there or your program won’t
run. Immediately before any method name, you must include a return type. This main method does not return
a value, so its return type is “void.” Again, you will get more information on return types later.

The main method must be called “main”. In Java SE applications like this one, the main method is the “kick
off” point for the application. In other words, it is from the main method that the JVM will start to execute
the application.

Java is case sensitive, so the method cannot have the name “Main”, “MAIN”, or “mAiN”.
Further, the main method must take an argument (also called parameter) that is an array of Strings. Notice the
“String[] args” in parenthesis after the method name. This is the type and name of the argument. “String”
indicates that the argument is of type String, and the [ ] indicates it is also an array. You will learn more about
arrays later too. This array of Strings can be named anything you want; conventionally, it’s called “args”.

The main method (like most of Java) is very picky. Below are some examples of how the main method could
have been written. Which of these works (i.e., compiles and runs) and which won’t?


public static void main(String[] args)
static public void main(String command_Line_Arguments[])
public void static main(String[] args)
public void main(String args[])
public static void main(String args)
public static void Main(String[] args)


Curly brackets { } designate the beginning and end of a code block.  In the above code, a set of curly brackets
was used to surround the class. Within the start { and the end } of the class, one method was also defined.
Notice this method has its own start and end brackets.

Compile this code using the tools provided by the JDK.  Specifically, compile HelloWorld.java with javac
(pronounced “Java-C”). Javac is an executable (javac.exe) you can find in your JDK’s bin folder. To call it,
open up a Windows command prompt and type the following command:


c:\jdk1.5\bin\javac c:\myDir\HelloWorld.java


Most Java developers add the JDK’s bin folder to the path of their operating system. If your operating
system is set up correctly, you should just be able to type javac without the path to the JDK’s bin folder.
Therefore, if you are in the folder containing HelloWorld.java, you can simply run the compile command as
shown below:


c:\myDir>javac HelloWorld.java


Many things can go wrong with your first compile. If set up incorrectly, the command javac won’t be found,
in which case you need to modify the path or environmental variable in the OS. If you mistyped any part of
the Java file, the compile might fail, leaving you with compiler errors to fix.

Below are three rather common Java syntax mistakes. What is the error telling the developer?


HelloWorld.java:3: Identifier expected.
       public static void main(String[] args){
                    ^

HelloWorld.java:4: Undefined variable or class name: out
               out.println("Hello World");
               ^

helloWorld.java:1: Public class HelloWorld must be defined in a file
called "HelloWorld.java".
public class HelloWorld{


As you learn and practice coding Java, compiler errors become easier to read.

Recall that the result of running javac is bytecode. More precisely, the result of compiling a Java file is a
bytecode file. Bytecode files have the same name as the Java source code file with a “.class” suffix. Thus
bytecode files are often called class files. If you were to open a class file with a text editor, most of it would
be illegible.

















Remember, a bytecode file contains “machine code” for the JVM. Although not exactly machine code, it is
also not simple text.

To run an application, use the java command.


c:\myDir>java HelloWorld


The command java is referencing java.exe, which is part of the JDK and can also be found in the \bin folder.
The result of the run should look something like the following:
















It’s not too exciting to look at, but if you look at what’s going on behind the scenes, quite a bit is happening.
Your First Java Program
Table of Contents
Copyright (c) 2008.  Intertech, Inc. All Rights Reserved.  This information is to be used exclusively as an
online learning aid.  Any attempts to copy, reproduce, or use for training is strictly prohibited.
Courseware
Training Resources
Tutorials
Services