A Hello World program is a basic and straightforward application that displays “Hello, World!” on the screen and it’s a great way to start writing your first code in java.

1. Getting Started

To start writing your first program in java. Access your preferred IDE and create a file Main.java and copy the following code and then run it.

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

Congratulations! You just wrote and run your very first java program.

2. Breaking it down

Great! However, you have no idea what you just wrote. So let’s break it down one line at a time.

public class Main{
  


}

The following line will define a class called Main, we will learn more about classes later on so no need to worry about it right now. The most important thing is to have the class name always match the code file name. For example, if your file is Main.java, you should write public class Main. If your file is hello.java, you should write public class hello. Every opening curly brace { in java should be matched with a closing curly brace } .

public class Main{
    public static void main(String[] args) {
        
    }
}

Every java program needs to know where it starts execution. The public static void main line is java’s way to know that this is where it starts executing the code. Anything you want to execute when the program should go inside.

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

Finally, we must instruct the computer to print the message “Hello, World!”. We can achieve this by using System.out.println(). The message to print should come inside the parenthesis and should be included within double quotations “”. For example, if you want to print the sentence Thank you! you would write System.out.println(“Thank you!”). Every instruction that does not require a curly brace should end with a semicolon ;

3. Exercises

Other Lessons in Introduction to Programming in Java

No. Lesson Reading Time
1 Introduction to Java 5 mins
2 Setting up the environment in Java 5 mins
3 Java Hello World Program 5 mins
4 Java Output 5 mins
5 Java Escape Characters 5 mins
6 Variables & Types 5 mins
7 Arithmetic Operators in Java 5 mins

Related Subjects

or view all subjects
No realted subjects found.