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 before 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

  • Break out of nested loop in java
    22 July

    How to break out of nested loops in Java

    Table of ContentsUsing break (will break inner loop)Using named loopUsing named blockUsing returnConclusion In this post, we will see how to break out of nested loops in Java. Using break (will break inner loop) It is very important to understand how nested loops work to ensure that applying break will output the desired result. If […]

  • public static void main(String args[]) - Java main method
    11 June

    public static void main(String[] args) – Java main method

    Table of ContentsWhy is the main method so important?SyntaxExplanation of the KeywordsExamples of Java main method ProgramsSimple Java ProgramPassing arguments in the main methodNon-Public main method.Defining a return value for the main methodOverloading the main methodQuestionsCan we run java without main method?Can we have more than one main method in classConclusion If you have worked […]

  • Java projects for beginners
    30 April

    Top 20 Java Projects for Beginners

    This tutorial provides the top 20 java projects for beginners for practice. These projects will help you to learn java quickly and efficiently.

  • How to end program in java
    08 March

    How to end program in java

    Table of ContentsUsing System.exit() to end java programExample to demonstrate System.exit() methodHow to end java program in an if statement in javaUsing return to end current method Ho In this post, we will see how to end program in java. You can use System.exit(0) to end program in java. Using System.exit() to end java program […]

  • 25 September

    Reference Variable in Java

    Table of ContentsReference variableStatic Reference VariableInstance Reference VariableLocal Reference Variable 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 […]

  • 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 […]

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.