HelloWorld Program in Java - Example and Explanation

When I first started with Java programming, the fist program I wrote was HelloWorld in Java. That time I just copied it from a text book we were referring, I didn't know anything about it. It took me a lot of time to understand everything about HelloWorld in Java and how it works. From that time, I made it a rule to start with writing HelloWorld program in any new programming language I learn and try to understand every single element of it in as much detail as possible, but,  to be frank, its not that easy. When you write say HelloWorld in Java,  you are not capable enough to understand everything about it, and I found it better that instead of learning everything in one go, you should give yourself some time to let the knowledge you learned sink in. It's even better if you write HelloWorld program multiple times without looking at  the book. I have found that doing it repeatedly helps you to understand it better.

So, why after so many years I have decided to write about Helloworld program in Java? and what is the meaning of different components of it? Well, one of my reader who has just started learning Java asked me this question. He was curious to understand what does System.out.println() really means and why we didn't wrote something like println() and requested me to write about it.

I also realized that since many beginners visit my blog, its fair to them to write tutorials which explain basics right from HelloWorld in Java. By the way, if you are serious about learning Java, then there is no substitute of a good book. You will learn more in less time by following a quality book like Head First Java 2nd Edition.

Helloworld Example in Java

Before I explain you about Helloworld program, let's first write it down. I love to write it because its very easy to write but satisfaction it gives is immense, because you see something running. This also means you have got all your setup right, which is very important for a beginner, who spent countless hours installing JDK and getting PATH and CLASSPATH right.

For those, who wants to use IDE like Eclipse and Netbeans right from the word go, its very easy to write Helloworld and execute it but for those hardworking Java beginners who wants to use notepad and command prompt, running helloworld is long journey.

I am assuming you have installed Java in your machine and you have also included JDK binary in PATH environment variable. If you have any problem doing those, I suggest you to read my article how to install JDK in Windows 8  and how to setup PATH and CLASSPATH for step by step guidance, and remember you can always comment and ask if you face any issue which you are not able to solve or want to understand more about it.

You can also check out Cay S. Horstmann's all time classic Core Java Volume 1 - Fundamentals to learn more about each element used in this Java HelloWorld program.

Java HelloWorld Example

Now,  let's write our first program, HelloWorld in Java :

 * Helloworld in Java, your first Java Program
 * @author Javin Paul
public class HelloWorld {

  public static void main(String args[]) { 
  System.out.println("Helloworld in Java");



I am using Eclipse to write it again but I suggest you guys to use notepad and command prompt because that's where you understand How PATH and CLASSPATH works in Java. So, if you are writing it in notepad then make sure you follow below steps :

1) Save your HelloWorld program into a file also name as HelloWorld.java, name of your public class should exactly match with the name of file.

2) First compile your HelloWorld.java file to create class file, for that just go to the directory where you have saved your HelloWorld.java file and execute following command :

$ javac HelloWorld.java

If you get error "javac is not recognized as an internal or external command" it means your PATH is not set properly and system is not able to look for "javac" command it need to compiler Java file. Its usually due to missing JDK bin directory in PATH and I suggest you to read that article to sort it

3) If you able to compile the Java source file then you will see a class file "HelloWorld.class" file in same directory. It means half job is done, now you just need to run it. In order to run a Java program, you need to use "java" command as shown below :

$ java HelloWorld

Remember there is no extension, its just the name of your public class which contains main method. If everything goes well then you will see message "HelloWorld in Java" in your screen or console. If you get error "Could not find or load main class" then you need to check out that article to find the exact reason. This usually happens when you use package to store your class but in this article, I have not used package so you will very unlikely to get that error.
HelloWorld Example in Java

Understanding HelloWorld in Java

There are many things in this program to understand, most important of them is the class itself and then you have main method and then our beloved out Sytem.out.println() method, so lets start with class itself.

1) The public class
Java is an object oriented programming and you cannot run your code without a class in Java. So you need to put your code inside a class. In Java, public is an access modifier, which means your class is accessible to every other class. You also need to make sure that name of your Java source file must be same as your public class. Also, you cannot have more than one public class in a Java source file, though you can have more than one class. Only one can be public, rest of them can be without any access modifier.

2) The main method
This is entry point of your Java program. When you run the "java" command, it creates a JVM and then JVM will call main() method of the class you supplied as argument, in our case its "HelloWorld". Now every single word in public static void main (String args[]) has their meaning. e.g. why public, why static or why void. I have already answered all these question in my earlier post Why main is public static and void in Java.  You can read that article to understand it bit more.

3) The String array
If you notice argument to main method is a String array. Whatever argument you pass to your program (anything at command prompt after name of main class) will be stored in this array. Arguments are separated by white space, so if you pass two arguments as shown below, array will contain two elements :

$ java HelloWorld firstArg secondArg

You can access this argument directly from array as args[0] and args[1]. You can also declare this array as variable argument post Java 1.5 and you can read more about this String array in my earlier post, understanding String array of main method in Java.

4) The System.out.println()
This is one of the most important and useful thing to understand from your HelloWorld program because you will often use this line of code to print something on console. In this code, we are actually calling a method called println(), which accepts a String and prints it on console and also adds a new line character. Now there is more on this code, System is actually a class in java.lang package for System related stuff e.g. getting access to console,  dot (.) is an operator to access members of a class e.g. fields and methods, which means out is a member of System class, which is true. Actually out is an object of PrintStream class which defines println() method and that's why we need out to call println() method in Java.

That's all in this article about understanding Helloworld in Java. Its your time now to try and run this program. If you face any issue, whether related to code or any environment issue related to PATH and CLASSPATH or you want to understand more about something, you can always ask here.

Welcome to Java world and Happy learning Java !!.

Further Learning
Data Structures and Algorithms: Deep Dive Using Java
Java Fundamentals: The Java Language
Complete Java Masterclass

1 comment:

Feel free to comment, ask questions if you have any doubt.