5 ways to Print “Hello World” in Java

Java Programming Ways to Print “Any Statement” :

Hello Guys, Now we are going to discuss that 5 ways to Print an statement in Java. We only know the 1 or two ways but In this article you will see all 5 way to print “Your Statement” in Java Programming.

In This Coding Example, I have created the Class “WaysToPrintHello”, You can take any name. All have been Commented Here:

First Way: Using Print Statement in Main Method:

Java System.out.println() is used to print an argument that is passed to it. The statement can be broken into 3 parts which can be understood separately as:

System , out and Println() , See Below:

  • System: It is a final class defined in the java.lang package.
  • out: This is an instance of PrintStream type, which is a public and static member field of the System class.
  • println(): As all instances of PrintStream class have a public method println(), hence we can invoke the same on out as well. This is an upgraded version of print(). It prints any argument passed to it and adds a new line to the output. We can assume that System.out represents the Standard Output Stream.

Syntax:

System.out.println(parameter)

Parameters: The parameter might be anything that the user wishes to print on the output screen. Like Here the Parameter is “This is my First Way of Program”

public class WaysToPrintHello {

// Class is WaysToPrintHello
    public static void main(String[] args) {
        System.out.println("This is my First way of Program");
        //This is the way that we all know, Using Print Statement in Main Method
    }
}
Second Way: Using Default Constructor

In Java, a constructor is a block of codes similar to the method. It is called when an instance of the class is created. At the time of calling constructor, memory for the object is allocated in the memory.

It is a special type of method which is used to initialize the object.

Every time an object is created using the new() keyword, at least one constructor is called.

It calls a default constructor if there is no constructor available in the class. In such case, Java compiler provides a default constructor by default.

There are two types of constructors in Java:

  1. no-arg constructor, and
  2. parameterized constructor.

Note: It is called constructor because it constructs the values at the time of object creation. It is not necessary to write a constructor for a class. It is because java compiler creates a default constructor if your class doesn’t have any.

Rules for creating Java constructor:

There are two rules defined for the constructor.

  • Constructor name must be the same as its class name
  • A Constructor must have no explicit return type
  • A Java constructor cannot be abstract, static, final, and synchronized

A constructor is called “Default Constructor” when it doesn’t have any parameter.

Syntax of Default Constructor:

<class_name>()
{
 // Statement
}

Here WaysToPrintHello() is defined as a Default Constructor.

public class WaysToPrintHello {
    WaysToPrintHello()
    {
        System.out.println("This is my Second way of Program");
    }

    public static void main(String[] args) {
        WaysToPrintHello obj1 = new WaysToPrintHello();

    }
}

Third Way: Using Parameterized Constructor

Parameterized Constructor β€“ A constructor is called Parameterized Constructor when it accepts a specific number of parameters. To initialize data members of a class with distinct values.

In the below example, we are passing a string to the object. The constructor then initializes the value of s using the passed values. Display method then gives the desired output.

With a parameterized constructor for a class, one must provide initial values as arguments, otherwise, the compiler reports an error.

public class WaysToPrintHello {
    WaysToPrintHello(String s)
    {
        System.out.println(s);
    }

    public static void main(String[] args) {
        WaysToPrintHello obj1 = new WaysToPrintHello("This is my third way of program");

    }
}


Fourth Way: Using Member Function

The functions declared inside the class are known as member functions.

Member functions are methods or functions that are defined inside of objects. Generally used to manipulate data members and other object data.

Example is illustrated Below:

public class WaysToPrintHello {

    void display()
    {
        System.out.println("This is my Fourth Way of Program");
    }

    public static void main(String[] args) {
        WaysToPrintHello obj1 = new WaysToPrintHello();
        obj1.display();

    }
}


Fifth Way: Using Passing Argument

Here we have defined Member Function display() and passing a string argument which is String Value in s.

public class WaysToPrintHello {

    void display(String s)
    {
        System.out.println(s);
    }

    public static void main(String[] args) {
        WaysToPrintHello obj1 = new WaysToPrintHello();
        obj1.display("This is my Fifth Way of Program");

    }
}

So Hope you Learnt all the 5 ways to write your Statement in JAVA programming.

You Can also watch this Video:

Loading