Java || Hello World!

Print Friendly, PDF & Email

This page will consist of creating the typical “hello world!” application. First, you will need to create a new project in whichever IDE you are using. I am using a simple text editor and the terminal, but any compiler will do.

When you have everything loaded up, you will start your code off by entering the headers like so into your file.

The most basic form of a class definition is shown above. The keyword “class” begins the class definition for the class named “HelloWorld.”

Next, you are going to add the main function. This will generally look something like this:

Every program in Java must contain a main method declaration as shown above. The declaration “public” and “static” can be written in any order (public static or static public), but the custom is to use “public static.”

We are halfway finished, now all we have to do is say hello to the world! You will do that by adding this line of code to your program, which basically outputs data to the screen

The above code uses the “System” class from the core library to print the “Hello World!” message to standard output.

So when you add everything together, your full code will look something like this


Once you have loaded the terminal, if you have your java source file saved in any directory other than the home folder, you will need to change directories to that specific folder where your source file resides. Many typically save their source files to the desktop.

*** If your files are located on the desktop, in the terminal, type this command:

@ubuntu:~$ cd Desktop

*** Next we will compile the java source file, and this can be achieved by typing the following command:
(Notice the .java source file is named exactly the same as the class header)

@ubuntu:~/Desktop$ javac

*** To run the compiled program, simply type this command:

@ubuntu:~/Desktop$ java HelloWorld

And there you have it! After you compile the above code, you should get this as your output

Hello World!

Was this article helpful?
👍 YesNo

Leave a Reply