Table of Contents
Java language Hello world program
In this tutorial, we will discuss the Java language Hello world program
Hello world is usually the first easy to understand program used to introduce programming language for beginners.
Firstly, we will understand how to write hello world program and then how to compile and run the program with no error.
To successfully compile and run the program, we want text editor (Notepad) or IDE and also make sure java is installed properly.
Open the Notepad Text editor
Write the program as shown below.
Explanation of this program
Save the file using the same class name with extension .java
Hello world program is successfully compiled and run with the expected output.
Output
Ok, let’s learn the above program
//Hello world program in Java
//Your first java program
Here is an explanation for starting the program using comments(single line comment). Comments are intended for users reading the code to understand the purpose and functionality of this program.
Comments are completely ignored by the java compilers
class Hello_world{
……………………….
……………………….
}
Every java program must begin with class declaration in Java. In the program, class is a keyword in java. Hello_world is the name of the class.
public static void main (String args[])
{
// body of main method with Helo world code
}
This is a java main method with method body.Every java program must contain a main method.
System.out.println(“Hello world”);
The above code displays the string(Hello world )inside double quotation marks on your screen.
This statement is inside the main method in java. The main method is inside the class declaration.
class Hello_world{
public static void main (String args[]){
System.out.println(“Hello world”);
}
}
Display hello world program in different ways in java.
We can write hello world program in different ways in java.
This is a simple hello world program in java that was created using user defined methods.
Program 1
Output
Hello world
Explanation for program 1
You can do it
- Open a notepad in text editor.
- Declare a class in java with pair of curly brackets({}).
- Declare a main method inside the class definition.
- Declare a user defined method named hello() inside the class definition.
- Write a output statement inside the hello() method definition.
- Finally, call the method inside the main method in Java.
Program 2
This is a program that is using constructor for hello world program.
Output
Hello world
You can do it
- Open a notepad in texteditor.
- Declare a class in java with a pair of curly brackets({})
- Declare a constructor named hello_1() (same name of class) inside the class definition.
- Declare a main method inside the class definition.
- Write an output statement inside the hello_1() constructor definition.
- Finally create an object inside the main method(when we create object, constructor called automatically )
Program 3
This is a program that is using user defined methods to create objects.
Output
Hello world
Explanation for program 3
- Open a notepad in texteditor
- Declare a class in java with pair of curly brackets({}).
- Declare a method named hello() inside the class definition.
- Declare a main method inside the class definition.
- Write an output statement inside the hello() method definition.
- Then create an object inside the main method.
- Finally call the method hello() inside the main method.
Related post