×

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

Java InetAddress class

InetAddress class The InetAddress class refers to the IP address, both IPv4 and IPv6.An instance of an InetAddress consists of an IP address and possibly its corresponding hostname. It provides a method to get the...

9 minutes read.

How to Convert String to Date in Java

How to Convert String to Date in java You can convert String to Date in Java by using the parse() method. There are two classes which have parse() method namely, DateFormat and SimpleDateFormat classes....

2 minutes read.

Set Value to Enum in Java

In this article, you will be acknowledged about Enum in java. Most importantly you will learn how to set value to Enum or how to practically customize a value to...

3 minutes read.

Binary Strings Without Consecutive Ones in Java

N, an integer, is provided. Our objective is to determine the overall number of strings with length N that do not include successive 1s. Example: Input: 3 Output: 6 Explanation The binary forms of length...

6 minutes read.

String Palindrome Program in Java

String Palindrome Program in Java The palindrome is a string, phrase, word, number, or other sequences of characters that can be read in both directions i.e. forward (left to right) and...

11 minutes read.

Selection Sort in Java

Selection Sort in Java Selection sort is also a simple sorting algorithm that works by repeatedly finding the minimum element from the unsorted portion of the input array, and placing it...

5 minutes read.

Java String compareTo() Method

compareTo() method is used to compare the two specified Strings based on the alphabetical order(lexicographical order) of their characters.It returns positive number ,negative number or 0 Syntax: public int compareTo(String anotherString) Parameters: anotherString: the...

2 minutes read.

Java Comparable Interface

We implement comparable interface when we need a new logic for determining equality. if two objects are equal, the equals() method returns true and compareTo() method returns 0. The basic...

1 minute read.

Java Subtract Days from Current Date

Dealing with date and time in Java is not a particularly challenging operation because Java has an API for date and time that simplifies duties for developers. There are two...

3 minutes read.

Java For Loop

A for loop is used to execute a set of statements for a fixed number of times. It takes the following form: for (initialization; condition; update) { statements; } The for loop defines...

2 minutes read.

Lazy Propagation in Segment Tree in Java

The topic of segment trees in Java is continued by the topic of sluggish propagation in segment trees. It is suggested that readers first read through the section tree topic....

4 minutes read.

Java Integer compareUnsigned() method

The compareUnsigned() method of Integer class compares two int objects numerically by treating the values as unsigned. Syntax public static int compareUnsigned(int x , int y) Parameters The parameters ‘x’ and ‘y’ represent the...

1 minute read.

Array and String with Examples in Java

Array in Java: An array in Java is a group of variables with similar types that have a common name. The arrays used in Java differ from those used in C/C++. Key...

6 minutes read.

Missing Number in an Arithmetic Progression in Java

Given an array that shows the elements of an orderly arithmetic progression. Find the missing number to complete the succession of elements. Example:  Input: a [ ] = {2 , 4 , 6...

3 minutes read.

Packages Program in java

Let us know what package is in the Java programming language, and later we will learn about types of packages and how to define a package. How to import Java...

4 minutes read.

Java Integer max() method

The max() method of Integer class returns the greater of two int values. It returns the same result as by calling Math.max. Syntax public static int max(int a, int b) Parameters The parameters ‘a’...

2 minutes read.

Dutch National Flag Problem in Java

Dutch National Flag (DNF) is a programming issue that Edsger Dijkstra put up. The white, red, and blue hues make up the Dutch flag. The goal is to haphazardly set...

6 minutes read.

How to run java program in ubuntu

To run the java programming in ubuntu, we need to follow several steps: Let's see the process. Step1: Install the Java compiler or JDK to the system. To run the java program, we...

3 minutes read.

Java Strictfp Keyword

Strictfp is used to impose limits on floating-point calculation. It ensures that we will get the same result on every platform while performing an operation with the floating-point variable. The floating-point calculation is platform-dependent due...

1 minute read.

Pernicious Number in Java

If the number of 1s in a number appearing in the binary representation is the prime number, such a number is known as a pernicious number. A pernicious number always corresponds to...

4 minutes read.