Writing your first Java code, “Hello World”

Hello there, welcome to our tutorial series of Beginner’s Java programming. Today we are going to build our first program “Hello World”.

Installing Java and Eclipse

In order to run a Java program, you must have to install Java Runtime (jre) on your computer. Also, if you want to write Java programs, you have to have Java Development Kit (jdk).

Jdk has jre in it so, you just have to download jdk only.

For that go to http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html and download and install the relevant jdk for your operating system.

Once you install jdk, you can write java programs on notepad and run it using the command prompt or the terminal. But that’s too complicated for now.

So, we have to download another software where we can write programs. Eclipse is the software we want. There are other softwares like NetBeans as well. But for now, let’s do our coding in Eclipse.

To download eclipse, go to https://eclipse.org/downloads/ and get the latest version of it. Both jdk and eclipse are FREE! Don’t need to pay for that 🙂 Then follow the steps for installing eclipse.

Writing your first application 🙂

So, first, go to the place where you have installed eclipse and launch it. It will ask to give the location for a new workspace.

This is the place where all your code is saved. So give it a suitable location.

Then, a welcome window appears.

Select, “Create a new Java Project” from the window.

Then type “JavaTutorials” as the project name and press OK.

Now you will see a window like this.

Next, right click on the JavaTutorials folder and select newClass.

Give “HelloWorld” as the new Class name and press OK.

Now the eclipse window should look like this.

Then, type the following code in the same way as given in the figure.

Finally, let’s run our first Java program!! Click on the green play button at the left corner of the toolbar.

A new window will appear and ask you to save the content before launching. Press OK for that.

If you did all the steps correctly, then the text Hello World! should appear on the console as follows.

Now you can play with it. Just try to type anything instead of “Hello World!” in the System.out.print statement. But remember to add quotation marks.

For example, try this;

Writing Multiple Lines

You can add many sentences. For that, you have to type this again and again.

If you do this correctly and run, then those statements will be written in the console window.

Writing on a New Line

If you want to start with the next statement with a new line, just type println instead of print.

See, the first statement is changed from print to println. Now this ln means line. So, print line is the meaning.

print” is the term used by Java to tell the computer to write the text on the console.

println” will add a line feed after writing the text. That means, the next print statement will display the text in a new line.

You can get familiarize with this by experimenting 🙂

Another example. Can you guess the output?

That’s enough for the Hello World. Now we have to get down to the serious programming. So, let’s start our journey. Welcome on-board! 🙂