Java - Hello World Program
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
}
}
Explanation 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 Class
public 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 Main
public 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 Used
Let's check the purpose of each keyword in this line.
public − defines the scope of the main method. Being public,
this method can be called by external program like JVM.
static − defines the state of the main method. Being static, this
method can be called by external program like JVM without first
creating the object of the class.
void − defines the return type of the main method. Being void,
this method is not returning any value.
main − name of the method
String []args − arguments passed on command line while
executing the java command.
5. System.out.println() Method
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.
Ways to read input from console in Java
In Java, there are four different ways for reading input from the user in the
command line environment(console).
1.Using Buffered Reader Class
This is the Java classical method to take input, Introduced in JDK1.0. This method
is used by wrapping the System.in (standard input stream) in an
InputStreamReader which is wrapped in a BufferedReader, we can read input
from the user in the command line.
The input is buffered for efficient reading.
The wrapping code is hard to remember.
Implementation:
// Java program to demonstrate BufferedReader
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Test {
public static void main(String[] args)
throws IOException
// Enter data using BufferReader
BufferedReader reader = new BufferedReader(
new InputStreamReader(System.in));
// Reading data using readLine
String name = reader.readLine();
// Printing the read line
System.out.println(name);
}
To read other types, we use functions like Integer.parseInt(),
Double.parseDouble(). To read multiple values, we use split().
2. Using Scanner Class
This is probably the most preferred method to take input. The main purpose of
the Scanner class is to parse primitive types and strings using regular
expressions, however, it is also can be used to read input from the user in the
command line.
Convenient methods for parsing primitives (nextInt(), nextFloat(), …) from the
tokenized input.
Regular expressions can be used to find tokens.
The reading methods are not synchronized
// Java program to demonstrate working of Scanner in Java
import java.util.Scanner;
class GetInputFromUser {
public static void main(String args[])
// Using Scanner for Getting Input from User
Scanner in = new Scanner(System.in);
String s = in.nextLine();
System.out.println("You entered string " + s);
int a = in.nextInt();
System.out.println("You entered integer " + a);
float b = in.nextFloat();
System.out.println("You entered float " + b);
3. Using Console Class
It has been becoming a preferred way for reading user’s input from the command
line. In addition, it can be used for reading password-like input without echoing
the characters entered by the user; the format string syntax can also be used
(like System.out.printf()).
Advantages:
Reading password without echoing the entered characters.
Reading methods are synchronized.
Format string syntax can be used.
Does not work in non-interactive environment (such as in an IDE).
// Java program to demonstrate working of System.console()
// Note that this program does not work on IDEs as
// System.console() may require console
public class Sample {
public static void main(String[] args)
// Using Console to input data from user
String name = System.console().readLine();
System.out.println("You entered string " + name);
}
4. Using Command line argument
Most used user input for competitive coding. The command-line arguments are
stored in the String format. The parseInt method of the Integer class converts
string argument into Integer. Similarly, for float and others during execution. The
usage of args[] comes into existence in this input form. The passing of information
takes place during the program run. The command line is given to args[]. These
programs have to be run on cmd.
// Program to check for command line arguments
class Hello {
public static void main(String[] args)
// check if length of args array is
// greater than 0
if (args.length > 0) {
System.out.println(
"The command line arguments are:");
// iterating the args array and printing
// the command line arguments
for (String val : args)
System.out.println(val);
else
System.out.println("No command line "
+ "arguments found.");