Write First Java program - Hello World


Write below program and save it in 'HelloWorld.java' file.

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

Compile the Hello World Java program

Suppose you have saved HelloWorld.java in 'G:/programs' directory.
Open the command prompt and navigate to the above directory.
Run below command :

G:\programs>javac HelloWorld.java

Note: If running above command give error 'javac is not recognized as an internal or external command', then please set 'Path' environment variable properly, as shown in previous tutorial.
Once the program is compiled, you can see HelloWrold.class file created in same directory.


Run/Execute the HelloWorld program

G:\programs>java HelloWorld G:\programs>Hello World!

Note: If you try to run the program from any other location you will get an error. To resolve that you need to set classpath. See below figure to know the error and how to set classpath. Actually the 'java' command need a .class path. It search for required .class path at current directory and all directories mentioned in 'classpath'.

java_classpath


Hello World Program Explanation

    Though some part of the program will be explained in later tutorials, here are some very important points about it:
  1. First line of above program creates a new class named 'HelloWorld'. You can give it any name, but make sure whatever is name of this class, the name of file must be exactly same.
  2. Third line implements a method - main method. This is special method in java and it must be written in exactly same way as above.
  3. Note: Java is case-senstive. So main is different from Main or MAIN.
  4. main() method is the starting point of execution of a java program i.e. when you run the java program using 'java HelloWorld', it will invoke the main method of 'HelloWorld' class. This class must either be default(as in above program) or public. Also name of file must be same as name of this class.
  5. main() method must be qualified as public static and void. No change here, except you can change order of public and static, so it can also be written as static public void main(......)
  6. main method must also have an array of Strings in parameter. Though name of parameter can by anything so you can replace 'args' with 'cats' or any string - maybe even ur name. 'String[] args' can be also be 'String args[]' or 'String[] cats', 'String names[]'
  7. The Fifth line of program gives a simple statement which just prints 'Hello World!' on terminal or console. Syntax of this line should also be same - you can change only the part inside double quotes " ".
  8. Note: 'S' of System is in capital as 'System' is a class.
Now you might get confused as to where you should have capital letter and where small letter. This is driven by java naming convention and is easy to remember once you learn java naming convention. In java name of anything will be in camel case. Camel case says - if there are multiple words in your name, first letter of each world will be capital. Like PayPal, SmartCard, CollegeStudent, BankEmployee. The only twist comes with first letter of name and here is the secret - first letter of a type (class, Interface) will be capital, whereas first letter of a variable or method will be in small case. Rest of name follows camel case.
In above program, there are 2 classes - HelloWorld and System. Also there are 2 methods - main() and println(). There is also a variable 'out'. out is variable of System class.
I hope now you can understand why some letters are in capital while others in small.

Practice points - do it yourself

Make below changes in your program and try to compile/run it. See what error, if any, you get. Also notice when do you get the error - at compile time or at runtime:
  1. Change name of the file and see what happen.
  2. Make the class public as - public class HelloWorld
  3. Make the class private as - private class HelloWorld
  4. Change something in main method - remove public or static.
  5. Try to change name of main method to simple() or Main().
  6. Change arguments of main method as public static void main(String names[])