- On
- By
- 0 Comment
- Categories: Basic, Java basic
Hello world in Java programming language
Hello world in Java programming language
In this tutorial, we will discuss the hello world in Java programming language.
What is the hello world program
The word Hello world cannot be forgotten by programmers as it is the first program learnt by programmers.
“Hello world” program is very easy to learn and understand. This program returns the simple output Hello world and is used to introduce programming languages to beginners.
Before you write a code in hello world program, you must ready your system for code writing, compilation and running.
Following are important to initialize hello world program
- Java is installed to your system – we will write hello world program in Java language
- You must set a path in your system for Java program – if you don’t set a Java path on your system, you cannot compile using Java compiler.
- You must have a text editor with IDE to write a java program – you want a platform that is viable for code writing, compiling, debugging and to get perfect output. E.g. text editor – Note pad, Eclipse, Netbeans etc
If you done with the above criteria, you can write your first program in hello world
To begin with, you can learn to write hello world program and how to compile and run the program without error
Java hello world program
public class Hello_world{ public static void main(String args[]){ System.out.println("Hello world"); } }
When the above code is executed, it produces the following results:
Hello world
Explanation of Hello word program
//Hello world program in Java //your first Java program class Hello_world{ //class declaration named Hello_world public static void main(String args[]){//main method in Java //It is predefined method in Java System.out.println("Hello world"); //Display statement in Java //It is ended with semicolon }//end of main method }//end of class
How to save and run this program:
This program is written on the notepad. After you complete the program, find the save button and click save. Then select the folder to save the program in.
Note – when you save the hello world program, class name and file name should be both the same.
Open the command prompt and select the proper path to your program(drive F: java folder-Hello_world.java file)
Then compile the program using the javac compiler
javac Hello_world.java
Then run the program
java Hello_world
This program is successfully compiled and run with the expected output
Output
Ok, let’s discuss every segment of this program
//Hello world program in Java
//your first Java program
This is just an explanation. Starting the program using comment (Single line comment) leads to easy reading, very simple understanding, ease of understanding of functionality as very well.
Because comments are completely ignored by the java compiler
public class Hello_world{
//Statements..
.............
}
public -you can access this class from any classes
Note – In java, every program must begin inside the class declaration in enclosing curly brackets. In this segment of code, the class is a keyword in Java language. Hello_world is a class name.
public static void main(String args[]){
//body of main method with Hello world code
}
This is the main method in Java with method body inside enclosing braces (curly brackets). Every Java program must contain the main method.
System.out.println(“Hello world”);
The above statements are used to display any string (“Hello world”) inside double quotation marks for display on your screen. In this program, this statement is inside the main method.
class Hello_world{ public static void main(String args[]){ System.out.println("Hello world"); } }
Suggested for you
Hello world program in C++
Hello world program in C
Hello world program in Python