News & UpdatesProgrammingWeb programming StoreMy Projects
Links
Affiliates

Java Tutorial – 02 – Hello World

Installing

Before you can program in Java you need to download and install the Java Development Kit (JDK) Standard Edition (SE) from Oracle’s website. Among other things, the JDK includes the Java compiler, the class libraries and the virtual machine needed to run Java applications. Oracle’s download page also has a link to obtain Netbeans bundled with the JDK. Netbeans is an Integrated Development Environment (IDE) that will make development in Java much easier. Alternatively, another free IDE you can use is Eclipse, or if you do not want to use any IDE at all a regular text editor will work just fine.

Creating a project

If you decide to use an IDE (recommended) you need to create a project, which will manage the Java source files and other resources. Alternatively, if you prefer not to use an IDE you can create an empty file with the .java extension, for example MyApp.java, and open it in your text editor of choice.

To create a project in Netbeans, go to the File menu and select New Project. From the dialog box select the Java Application project type under the Java category and click next. On this dialog box set the project name to “MyProject” and the name of the main class to “myproject.MyApp”. Change the project’s location if you want to, and then hit the Finish button to generate the project. The project’s only file, MyApp.java, will then open up, containing some default code. You can go ahead and remove all of that code so that you start with an empty source file.

Hello world

When you have your project and programming environment set up the first application you will create is the Hello World program. This program will teach you how to compile and run Java applications, as well as how to output a string to a command window.

The first step in creating this program is to add a public class to your MyApp.java source file. The class must have the same name as the physical source file without the file extension, in this case “MyApp”. It is legal to have more than one class per file in Java, but only one public class is allowed, and that name must match the filename. Keep in mind that Java is case sensitive. The curly brackets following the class name delimits what belongs to the class and must be included. The brackets, along with their content, is referred to as a code block, or just a block.

public class MyApp {}

Next, add the main method inside the class. This is the starting point of the application and must always be included in the same form as is shown below. The keywords themselves will be looked at in later chapters.

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

The last step in completing the Hello World program is to output the text by calling the print method. This method is located inside the built-in System class, and then another level down inside the out class. The method takes a single argument – the string to be printed – and it ends with a semicolon, as do all statements in Java.

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

Note that the dot operator (.) is used to access members of a class.

Code hints

If you are unsure of what a specific class contains, or what arguments a method takes, you can take advantage of code hints in some IDEs, such as Netbeans. The code hint window appears anytime you are typing code and there are multiple predetermined alternatives. It can also be brought up manually by pressing Ctrl + Space. This is a very powerful feature that gives you quick access to the whole class library and their members, along with descriptions.

Recommended additional reading:
Sams - Teach Yourself Java in 24 Hours