×

Hello Program in Java

Hello Program in Java

The Hello program in Java displays the word Hello on the console. It is the basic program in Java. In this section, we will learn different ways to create and display the word Hello on the console.

Using print() Method

Filename: HelloExample.java

public class HelloExample
{
// Main Method 
public static void main(String argvs[])
{
               // Displaying the word Hello on the console
               System.out.print("Hello");
}
} 

Output:

Hello

Explanation: In Java, we use the print() method to print on the console. We have used out object, which is of the class PrintStream, to invoke the print() method. System is the class name.

There are different variations of print() method like println(), printf(). Let us discuss them.

Using println() Method

Filename: HelloExample1.java

public class HelloExample1
{             
public static void main(String argvs[])
{
               // Displaying the word Hello on the console and
               // Forcing the cursor to move to the next line
               System.out.println("Hello");
}
} 

Output:

Hello

Explanation: Instead of the print() method, we have used the println() method. The extra letters ln, moves the cursor to the next line. Therefore, the following snippet of code

public static void main(String argvs[])
{
               System.out.println("Hello");
               System.out.println("Hello");
} 

Output

Hello
Hello

Using printf() Method

Filename: HelloExample2.java

public class HelloExample2
{             
public static void main(String argvs[])
{
               // Character array
               char str[] = {'H', 'e', 'l', 'l', 'o'};
               // Loop for displaying the word Hello
               for(int i = 0; i < str.length; i++)
               {
                               System.out.printf("%c", str[i]); // Displaying characters
               }
}
} 

Output:

Hello

Explanation: In the character array, we have stored every character of the word Hello. Then using the Java for-loop, we are iterating over each element of the given character array. In the above-written code, we have used the method printf() to display the word Hello on the console. This method is similar to the printf() function that is used in the C language.

Using Command Line Argument

String argvs[] mentioned as an argument of the main method (main()) is the command-line argument. We can easily pass values to the command-line argument. The following program illustrates the same.

Filename: HelloExample3.java

public class HelloExample3
{             
public static void main(String argvs[])
{
               // Loop for displaying the strings passed through command-line argument
               for(int i = 0; i < argvs.length; i++)
               {
                               System.out.println(argvs[i]); // Displaying Strings
               }
}
}
To Compile: javac HelloExample3.java
To Execute: java HelloExample3 Hello 

Output:

Hello

Explanation: We can compile the above program using the javac command. During the execution time, we have passed the word Hello as a command-line argument. Finally, we are iterating over the command-line arguments (which is a String array) and displaying the string elements.

By Taking Input From User

Filename: HelloExample4.java

// Importing the class Scanner
import java.util.Scanner;
public class HelloExample4
{             
public static void main(String argvs[])
{
               Scanner scr = new Scanner(System.in);
               String input = scr.nextLine(); // Reading input
               System.out.println(input);                // Displaying input
}
} 

Output:

Hello

Explanation: Using the Scanner class, we are taking input from the user. Then we are displaying the input using the println() method. If the user has input Hello, then the program displays the word Hello on the console.


Related Topics

&amp;&amp; Operator in Java

“ && ” is the conditional - And operator in Java. In Java, it is an example of a logical operator. In Java, the “ & ” operator has two...

3 minutes read.

How to check Date Null in Java?

In this section, we will be acknowledged about Date Null in Java. The date null in Java is an entity that is used when there is no specified value for...

3 minutes read.

Getting the Day from the Date in Java?

To extract the day's name from the date, we will write Java application. When dealing the date and time in Java, the following classes are used. Class for Calendars: This class is...

6 minutes read.

Java Integer rotateRight() method

The rotateRight() method of Java Integer class returns the value obtained by rotating the  2’s complement binary representation of the given integer value right by the specified number of bits. Syntax public...

1 minute read.

Java Program to Print Permutations of String

A string is given and you need to print all the possible ways for that string. Permutation is arranging the characters of a string to get outputs from the given...

3 minutes read.

How to write basic Java Programs

Java Basic Programs In this section, we will learn how to write basic Java programs. But first we need to take care of the following requirement list. To execute a Java program,...

4 minutes read.

Java Pass-by-Reference

In Java, passing parameters can be done using one of two fundamental methods. Pass-by-value is used for the first and pass-by-reference is used for the second. One thing to keep...

2 minutes read.

Memory Areas in Java

Let’s have a look at how memory management in Java works. We will be going to discuss how the objects get destroyed, the working of a garbage collector, and things...

5 minutes read.

Java Arrays

An array is an object that stores a collection of values. An array can store two types of collection data: objects and primitives. An array is a collection of values which...

2 minutes read.

Java Math incrementExact() Method

The incrementExact() method of Math class returns the argument incremented by one, throwing an exception if the result overflows an int or a long. Syntax: public static int incrementExact (int a)public static...

1 minute read.

Java class class

Java Class class instances are an executing Java application's implementation of the classes and interfaces. As well as, every Array is indeed an object that is common for all Arrays...

6 minutes read.

Java Math min() Method

The min() method of Math class returns the smaller of two arguments. The arguments can be of double, float, int or long data type. Syntax: public static double min (double a, double...

2 minutes read.

Switch Case with Enum in Java

From some conditions, the java switch statement executes one statement. Similar to the If-Else-If ladder statement, this can be Byte, short, int, long, enum, string, and some wrapper types like...

4 minutes read.

Java Boolean hashCode() Method

The hashCode() method of Boolean class returns the hash code for the specified Boolean object or the given Boolean value. Syntax public int hashCode()public int hashCode(boolean value) Parameters The parameter ‘value’ represents the value...

2 minutes read.

Java Math cosh() Method

The cosh() method of Math class returns the first hyperbolic cosine((e+e)/2) of a double value. Syntax: public static double cosh(double x) Parameters: The parameter ‘x’ represents the number whose hyperbolic cosine is to be...

2 minutes read.

Different Ways to Print Exception Message in Java

In this section, we will learn how to use various Java Throwable class methods to print exception messages. The Throwable class has the three methods listed below for printing the...

4 minutes read.

Java Math addExact() Method

The addExact() method of Math class returns the sum of the two arguments, throwing an exception if the result overflows a long or an int. Syntax public static int addExact (int x,...

1 minute read.

Java Primitive Data Types

Primitive data types are the simplest data types in a programming language. They’re predefined in the language. The names of the primitive types are quite descriptive of the values that...

2 minutes read.

How to take Multiple String Input in Java using Scanner class

Scanner class is a class which takes the multiple input data or the single input data through an objects and methods. The Scanner class will be available in the java.util...

3 minutes read.

Hierarchy of operators in Java

Operators are the most frequently used terminology in any area of programming, and it helps in various approaches to efficiently solve daily life problems by computer programming. The simple addition of...

4 minutes read.