Get Started with HelloWorld and HelloMe

Write your first application called HelloWorld

Find it in GitHub

To start writing your first application in Java, the first thing you need to know is that Java is an Object Oriented Programming language and all codes that you write are encapsulated in a concept called class. What is class? A class is a blueprint or definition from which individual objects are created.

Since the purpose of this section is just a simple command line application to print “Hello World!” text in console, I stop talking about OOP, class, object and other language specific concept and I jump to explain how the skeleton of a simple Java application like a HelloWorld in Java is.

Follow these steps to create your first application as HelloWorld:

1. Create a folder called java-practices or something like that in your home directory
$ mkdir ~/java-practices

2. Inside java-practic folder create another folder called hello-world

$ cd ~/java-practices
$ mkdir hello-world

3. Inside hello-world folder, create a file called HelloWorld.java using an editor like vi, vim or nano, emacs, atom, vscode, notepad, notepad++ based on your OS and your interest. Here I just used touch command to create an empty file in my OSX.

$ cd hello-world
$ touch HelloWorld.java

4. Put following content into the created HelloWorld.java file.

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

Here I used echo command to insert that content there

$ echo 'public class HelloWorld {
 public static void main(String[] args) {
     System.out.println("Hello World !!!");
   }
}
' >> HelloWorld.java

5. Now using javac command which is one the command inside your <JDK>/bin folder, you can compile .java files.

$ javac HelloWorld.java

6. If you list the directory that you created HelloWorld.java file and ran javac to compile it, you will see a file with the extension of .class which is the compiled and binary of the above source code .

$ ls
HelloWorld.class HelloWorld.java

7. Now you can run your Hello World application using following command

$ java HelloWorld
Hello World !!!

You can find the whole code in My Java Core Tutorial github repo here:
https://github.com/myjavacoretutorial/hello-world.git

Define your fist class

The first line which you start defining a class you need at least two words: class and the class name in front of it. The public keyword is optional and at the end you open a curly brace { for class code block.

public class HelloWorld {

In this line you have public keyword that indicates this class is a public class and can be accessible through all other classes even in different packages. To define a class you need to put class keyword before the class name. The next word is the class name that I named it HelloWorld in this example.
The class name should be the same as the .java file name that you define this class in it.
Usually Java programmers will follow Pascal Case naming convention to name the class names and Java file names.

Camel Case is the practice of writing phrases such that each word or abbreviation in the middle of the phrase begins with a capital letter, with no intervening spaces or punctuation. 

Pascal Case is a subset of Camel Case where the first letter is also capitalized.

Define main method

In the second line, you define the starting point of application which we call it main method. In Java syntax main method is a public and static method. It can accept an array of string as its inputs which maps to the command line arguments that you pass from command line when you execute your command line.
Refer to HelloMe sample to learn more. Finally it should return void.

public static void main(String[] args) {

Java is not like C/C++ that its main method can also return an exit code as integer. In case you set something else, you can compile it using javac but you will receive this error, when you run it.

$ java HelloWorld
Error: Main method must return a value of type void in class HelloWorld, please
 define the main method as:
    public static void main(String[] args)

public keyword indicates that this method is publicly accessible. If you missed to put public keyword to define your main method you will get following error:

$ java HelloWorld
Error: Main method not found in class HelloWorld, please define the main method as:
    public static void main(String[] args)
 or a JavaFX application class must extend javafx.application.Application


When you want to define a method to be accessible without necessarily instantiating the class that owns that method, such a utility method to convert a date to string or vice versa, you need to make that method static. Learn more about static method here.

$ java HelloWorld
Error: Main method is not static in class HelloWorld, please define the main method as:
    public static void main(String[] args)

As I mentioned above, the main method should also have an argument of String array (String[]). The name of the argument can be anything you like, however usually the java codes use args. But for type of the argument, if you define something else, for example if you miss to put arrays braces after String type, java says, it cannot find the main method, you will get following error:

$ java HelloWorld
Error: Main method not found in class HelloWorld, please define the main method as:
    public static void main(String[] args)
 or a JavaFX application class must extend javafx.application.Application

Print text in console

The third line is helping you to print “Hello World” in the console. Using a static method called System.out.println. Learn more about System.out.println here.

Hello to yourself in your second practice

In this example, you simply put your name as the input argument of your java application command line tool and it prints Hello Your Name in the console.

$ java HelloMe Nasim
Hello Nasim

To start learning the core functionality of a programming language, I highly recommend you to chose an editor such as atom or vscode and NOT an IDE such as IntelliJ Idea or Eclipse.

Here are the steps:

1. Create a folder. You can call it hello-me and change your current directory to it.

$ mdkir hello-me
$ cd hello-me

2. Here I used atom editor. Under hello-me folder run following command to open atom with having the current folder as an atom project.

~/hello-me/$ atom .

And you will see your project folder structure in atom editor like this:

3. Create a file called HelloMe.java. Right click, select New File menu

and enter HelloMe.java in “Enter the path for the new file” text input

4. Type following content there:

public class HelloMe {
     public static void main(String[] args) {
         System.out.println("Hello " + args[0]);
     }
 }

As you see, I defined a public class called HelloMe which is the same as java file HelloMe.java. It has a main method that should be public static and it does not return anything and it has void type for its return type. It also has an argument of String[] (string array) and I called that argument as args.
In the body of the main method, I am printing the word Hello with a space after that as “Hello ” and I concat this string with the first element of args array. The args array, as I mentioned before, holds the arguments that you pass to your application as java command line arguments. Like this

 $ java HelloMe arg1 arg2 arg3 argN

In this example, I just pick the first element of args which is the index 0 and it is reachable by args[0] and I contacted it with “Hello ” and I print the whole contacted string to console. Here is the code:

System.out.println("Hello " + args[0]);

5. Now using javac command compile HelloMe.java file and it will generate a .class file.

$ javac HelloMe.java
$ ls
HelloMe.class HelloMe.java

6. Using following java command, you can run your HelloMe command line alication, but this time you need to pass your name or somebody’s name or some text at least to run it:

$ java HelloMe Nasim
Hello Nasim

Otherwise, if you just run your application without passing anything, you will get an exception of java.lang.ArrayIndexOutOfBoundsException in line 3, because your application tries to get the zero index element value from args array using args[0] and since your args array is empty because you did not pass any input argument to your command, it will throw “java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0”

$ java HelloMe
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
     at HelloMe.main(HelloMe.java:3)

You can play with whatever you learned here, print some random text or think about another simple console or command line application.

Leave a Reply