First Java Program Hello World

We are going to learn a simple java program to print "Hello World" string on screen. Printing "hello world" string program is traditional first program for any new programmer while learning a new programming language.

This program will give us a brief overview of how to write and compile a java program and about the fundamental structure of a java program.


To write your first java program, you will need
  • The Java Development Kit (JDK).
  • A text editor (like notepad ) or IDE (like Eclipse).

Java Program to Print "Hello World" String

Here is the java program to print "Hello World" string on screen.

package com.tcc.java.tutorial;

/*
 First Java program to print "Hello World"
 String on screen
 */
public class HelloWorld{  
    public static void main(String args[]){ 
        // prints Hello World
        System.out.println("Hello World");  
    }  
} 
Output
Hello World

How to Write and Compile a Java Program

  1. Write
    • Open a text editor and type the above mentioned java program to print "Hello World".
    • Save this file as HelloWorld.java
  2. Compile
    • Open command prompt and go to your current working directory where you saved your HelloWorld.java file. Let's say you saved it as C:\programs.
    • Compile your code by typing "javac HelloWorld.java" in command prompt. Your program will compile successfully, If your program doesn't contain any syntax error.
  3. Execute
    • Now, execute your java program by typing "java HelloWorld" in command prompt.
  4. Output
    • You will see "Hello World" printed on your console.

Understanding Hello World Java Program

Let's analyse above program line by line.

  1.  /*
     First Java program to print "Hello World"
     String on screen
     */
    

    This is a multiline comment in java, similar to the multi-line comment in C++. They start with /* and end with */. Whatever is written in between them is a comment. These comments will be ignored by the compiler at compilation time.

  2. public class HelloWorld{
    Public is an access specifier, class is a java jeyword used for declaring a class and HelloWorld is the name is class. Name of class name must exactly match the name of program file. Don't worry, if you don't understand anything here, we will discuss about it in detail on later tutorials.

  3. public static void main(String args[]){
    Public is an access specifier, which means accessible by all. static is a java keyword, a static function can be called directly without creating object. void is the return type of main method. String args[] is the command line argument of main function.

  4. // prints Hello World
    It is a single line comment. It starts with "//" and ends with a newline character. Everything on a line after // is ignored by a compiler

  5. System.out.println("Hello World");
    It is used to print a string on screen in a separate line.

Points to Remember
  • A multiline comment starts with /* and ends with */ .
  • The name of the program file must match with the name of class.
  • Every java program must have one main() function from where program execution starts.