↑ Return to Java

Print this Page

Lesson 1: Hello World

Creating a New Project

In this introductory lesson we will examine a “Hello World” program. Most programmers will start here when they start any project. By being able to produce any kind of output the programmer then knows that everything is working properly so that he can begin more complicated processes without fear that the compiler itself is causing issues. So let us begin by first looking at the the entire “Hello World”. Here you can take a look at the Integrated Development Environment (IDE) that I use. I like Eclipse the best.

There are several out there, and many of them are rather good. This one is very user friendly and is the basis for many other IDEs. IDEs are much like real environments in that it is best to use what you are comfortable in. I like this environment because most of the work I do does not involve creating graphical user interfaces (GUI). If I were to use a GUI I would change IDEs to something like NetBeans which has a built in GUI designer. Lets begin by creating a new project.

To do that click on File>New>Java Project. Lets then name the project “Lesson 1”.

Creating a New Package

Then go ahead and click on “Finish”. Your window should close and you should be back to the main screen. Now take a look to the left side and there should be a new folder that says “Lesson 1”. Expand that folder then create a new package. To create the package right-click on the “src” folder. Then click on New>Package.

You should get a new popup. Just type “lesson1” into the name field then click on finish.

Creating a New File

Finally, the last step before getting started with actual programming is we need to create the class. To do this, right click on your new package Then click on New>Class.

Where it says name go ahead and call it “HelloWorld”. Since this is the first class in the package, it is important here to make sure the “public static void main(String[] args)” check box is checked.

Creating a Basic “Hello World”

Ok we can finally start programming our first program. Here is the program code for a “Hello World”.

package lesson1;

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

All this program does is print out the phrase “Hello World”. Its simple but it sets the stage for a few key fundamentals. Lets go through it one line at a time. The first line “package lesson1” just states the name of the package which the program resides in. In eclipse this is automatically generated, and is a required line. So once eclipse makes it, just leave it alone. The next line of code is the class header. This line names and sets the access to this class. Again if you did everything right it should already be generated. The third line is the main method. This is a very important line. Every program must have a main, and can only have one main. The reason it is important is that when you run your program, the first thing that Java does is looks for this main method. It then runs the main method’s code. Finally, the last line of code is what actually prints the phrase “Hello World”.

Ok, so lets run this program. To do this just click on the green circle with an arrow in it.

Finally, after all this work we can finally see the fruits of our labor. You should get a single line appear in the console window, and it should simply say “Hello World”. Congrats! You made your first Java program.

Things to Play With

Try changing the text in the quotes where it says “Hello World”.


Leave a Reply

%d bloggers like this: