Java Hello World Program

In this post, we will see how to write first java program. This post is intended only for java beginner.


hello world program will be a very simple program that will print Hello, World! to console.

Before running the program, you need to make sure java is properly installed on your machine.

Prerequisite for running a java program

  • Install the JDK if you don’t have installed it, download the JDK and install it.
  • set path of the jdk/bin directory. you can follow this link for setting the path

You can either write a program in IDE such eclipse or you can simply write in in text editor.

You need to save this file as HelloWorld.java.

How this java program works

Let’s see detailed explanation of Hello world program.

Class declaration

This is a comment in java and this statement will be ignored at run time.

As java is object oriented programming, every java application should have class definition.

  • Class declaration: A class declaration includes name and visibility of the class.class HelloWorld is declaration of class which includes keyword class, followed by identifier HelloWorld
  • Class declaration is followed by curly braces {}, which defines class’s definition.

Main method

This is called main method in java. This is entry point for this program.

public: This is access modifier which is used to define visibility. Here main method will be visible to all other classes.
static: static members belongs to class rather than any specific object. It simply means you can access the static members without object.
void: void is another keyword which defines return type of main method. It simply means main method does not return anything brefore terminating the program.
main: main is method name here.
String args[]: You can pass arguments to java program using args[] array. It is used to take user input using command line.

Print statement

System.out.println is used to print literals in double quotes("") to console. As we have passed "Hello, World!" here, it will print Hello, World! to console.

Semicolon

As you can see, each statement is terminated with a semicolon(;). You can put new lines or spaces in the code but statement has to be ended by semicolon.

Compile and run the program

If you run this program in eclipse ide, you can simply right click and on run as java application.
You can compile this java program using command line as below:
Open the command prompt and go to the location where you have saved HelloWorld.java

$ javac HelloWorld.java

You can run the program using command line as below:

$ java HelloWorld

When you run above program, you will get below output:

Hello, World!

When we execute java program, we need to give full classname without .java extension.

Exercise

You need to print Yeah!! I executed my first java program on console and name of the class should be MyFirstJavaProgram

Important points

Let’s go through some important points about Hello world program.

  • Any java source file can have multiple classes but it can have only one public class.
  • Java source file name should be same as public class name.That’s why I said above “you need to save this file as HelloWorld.java”.
  • When you compile the java file, it is converted to byte code with .class extension, so you should be able to see HelloWorld.class in the directory.
  • When you execute the program, jvm looks for main method and will execute it. You need to be very careful with signature of main method, otherwise program may throw java.lang.NoSuchMethodError: main.

Related Posts

  • 25 September

    Reference Variable in Java

    In this post, we will see about Reference variable in java. Reference variable A variable that holds reference of an object is called a reference variable. Variable is a name that is used to hold a value of any type during program execution. If the type is an object, then the variable is called reference […]

  • 26 March

    Global variables in java

    In this post, we will see how to define global variables in java. Unlike C/C++, there are no standard global variables in java but you can still define global variables that can be used across all classes. Global variables are those variables that can be accessed across all the classes. Java does not support global […]

  • 26 March

    Static block in java

    In this post, we will see about how to implement static block in java. Here are some important points about static block in java. Static block is used for static initializations of a class Static block is executed only once when either you create an object of the class or access static member of the […]

  • 26 August

    Java default constructor

    In this post, we will see about Java default constructor. Default constructor is the no arg constructor which is inserted by compiler unless you provide any other constructor explicitly. You won’t able to see it as it is present in class file rather than source file. Is there any difference between no argument constructor and […]

  • 17 June

    How to learn Java Programming

    Java is one of the most used programming languages in the world for developing the desktop application and web application.It has completed 22 years in the programming world. In this post, we will see how to learn java programming in depth and in most efficient way. Java has a huge learning curve, it is a […]

  • 28 May

    Introduction to Java Programming

    Java is one of the most used programming languages in the world for developing desktop application and web application. It has completed 22 years in the programming world. History of Java: Java programming language was developed by James Ghosling, Patrick Naughton, Mike Sheridan at Sun Microsystems Inc. in 1991. The initial name was Oak but […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.