First Java Program

On this section of my  java tutorial series we will be creating a basic java application that simply prints on the screen a text “Hello World”. If you are absolutely new to java programming better go through this tutorial. Even though this example is very easy and basic, there will be a lot of things to learn. Once you finish this section, you would be able to start coding right away.

Topics Covered

At the end of this tutorial we should be able to fully understand the following:

  • How to create a Java Application
  • Eclipse Navigation
  • Compiling of java program using eclipse
  • Java Program composition
  • Java Comments
  • Public Access modifiers
  • Print a simple message to console

How to create a Java Package

If you follow my previous tutorial in creating a java project using eclipse, a “Java Tutorial” project should already be available on your eclipse workspace. If you have not done so please create a new java project and named it “Java Tutorial”

Java Package

Before we move further let's tackle first what is a java package. Basically a package is a facility to organize classes that belongs to the same group or much more sense to say they have similar functionality. Like for example a utility for reading a file, then it is sensible to put it on the file package rather than putting it in database package. Think of a java package as a folder structure on your hard drive, there would be root directories and child directories. But please take note that there are some restrictions in accessing class files on different packages which we will be discussing further when we go to the access modifiers topic.

To create a java package navigate on your project and look for folder src. Right click on this folder and the following option should come up.

 

Just click on New –> Package. A new window would appear and it will ask for package name. In this case just put “com.javatutorial.example” as the package name. This would create a new java package. Click finish after putting the desired package name.

 

 

How to Create a Java Class

So far we have created a Java Project and also a new package. Let’s now create a java class.

 

 

On the left pane on our eclipse workspace, we should be seeing Java Tutorial Project and a package java.tutorial.example. Right click on the package name and an option will come out similar to what we did on the package creation but this time instead of selecting package we would be selecting Class. The following window would come out.

 

 

Populate the form accordingly. Leave the default value of the Source Folder and package name. As you would have noticed the default value corresponding to the Java Project and package where we add the new class. For the name field, put HelloWorld. Make a note that space is not allowed as Class Name. Ticked the public static void main(String[] args)  and leave all other field as it is Click Finish.

A new class will be generated with the name HelloWorld.

package com.javatutorial.example;

public class HelloWorld {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}

The above code would not yet do anything just a blank class. Let’s remove line 7, this basically just a comment which is basically just a stub entry reminding user to start adding code logic on that line.

After cleaning the code let’s put in the line that prints Hello World to the console. Add System.out.println(“Hello World”). Below is the full source code of Hello World, and then let’s go through each of the component.

package com.javatutorial.example;

/*
 * This is a demo program
 * that prints hello world
 * on the console
 */

public class HelloWorld {

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

	}

}

Line 1 is the package name where the class belongs. Check on the left side pane of your eclipse application, the class HelloWorld is inside the package com.javatutorial.example which matches the one that is declared on the first line of our class.

Line 3 up to 7 is a comment. This serves as a way to document what the class is all about. It is a good practice to put sensible information on comments.

For Line 9 public class HelloWorld, this where we declared what is our class name. Notice that we have started the declaration with a keyword public which means that our class HelloWorld is accessible anywhere. A java class is composed of method is inside it. In line 9, you can see that there is an opening bracket. This opening bracket needs to be close which we have done on line 17.

A java class can have a multiple methods. Treat a java method as a behavior of our java class. In this example, our method is main as have declared on line 11. Basically line 11 until 15 is our method on this class. Similar to our declaration of our HelloWorld class, each java method need to have a opening and closing bracket. Which means to say that inside the opening and closing bracket all the code that we put is part of the method main. Every time thatwe call this method, all the code logic inside it will be executed.

Lets take a look on our method declaration public static void main(String[] args) . Similar to our declaration of our class there is a public keyword before the method name. This means that all classes that has access to our class can also call this method. More on the access modifiers on the later section of our java programming tutorial. Now lets move on the keyword void. The void declaration before method name main is actually a return type. On this case, since we put void it means that this method would return nothing. The return type is varied from primitive data type to object classes. The word main on the above code snippet is the method name. Inside the opening and closing parenthesis is what we call method arguments which means this method is accepting an array of String with name args.

Now that we have discussed the basic java program structure let’s go in detail the code that accomplish our initial goal which is to print “Hello World” on the console. This is done by calling the static method println from the System class. The println method accepts String as it’s input and print it on the console. Because we want to print “Hello World” on the console thus we have declared System.out.println(“Hello World”); Dont’t forget to add semicolon after the closing parentheses. Each code declaration is terminated with semicolon, this is a must.

How to compile and run a java program in eclipse

So far we have covered all the basics of a java program. Let’s tackle now on how to run our java code.

Look for the play icon in green color on the top most part of our eclipse ide and click it. A set of options will pop out, select Run As –> Java Application