Printing "Hello World" on the output screen (console) is the first program in Java and other programming languages. This tutorial will teach you how you can write your first program (print "Hello World" program) in Java programming.
Java program to print "Hello World"Java program to print "Hello World" is given below:
public class MyFirstJavaProgram { /* This is my first java program. * This will print 'Hello World' as the output */ public static void main(String []args) { System.out.println("Hello World"); // prints Hello World } }Steps to Write, Save, and Run Hello World Program
Let's look at how to save the file, compile, and run the program. Please follow the subsequent steps −
C:\> javac MyFirstJavaProgram.java C:\> java MyFirstJavaProgram Hello WorldExplanation of Hello World Program
As we've successfully printed Hello World on the output screen. Let's understand the code line by line.
1. Public Main Classpublic class MyFirstJavaProgram {
This line is creating a new class MyFirstJavaProgram and being public, this class is to be defined in the same name file as MyFirstJavaProgram.java. This convention helps Java compiler to identify the name of public class to be created before reading the file content.
2. Comment Section/* This is my first java program. * This will print 'Hello World' as the output */
These lines being in /* */ block are not considered by Java compiler and are comments. A comment helps to understand program in a better way and makes code readable and understandable.
3. Public Static Void Mainpublic static void main(String []args) {
This line represents the main method that JVM calls when this program is loaded into memory. This method is used to execute the program. Once this method is finished, program is finished in single threaded environment.
4. Keywords UsedLet's check the purpose of each keyword in this line.
System.out.println("Hello World"); // prints Hello World
System.out represents the primary console and its println() method is taking "Hello World" as input and it prints the same to the console output.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4