Learn Java

Learn Java Print Variables

Display Variables – Java Print Variables

Use println( ) method is usually used to display whatever you can put inside.

If you want to combine both text and a variable, use the + character, and text always comes in these ” “:

 

Code Example:

public class first {


    public static void main(String[] args) {
        /*  Learn How to Use Println
        Println prints every println in different line  
        */


        String name = "Hanuman!";
        System.out.println("Name of the person is: " + name);
        System.out.println("Name of the person Writing 2nd time: " + name);


       


          /*  Learn How to Use Print
        Print prints every print in same line  
        */


        String name1 = "Sita!";
        System.out.print("Name of the person is: " + name1);


        String name2 = "Ram!";
        System.out.print(" Name of the person is: " + name2);


    }


}

 

The output of the above Code:

PS C:\Users\Priyanka\Documents\java> javac first.java
PS C:\Users\Priyanka\Documents\java> java first
Name of the person is: Hanuman
Name of the person Writing 2nd time: Hanuman
Name of the person is: SitaName of the person is: Ram
PS C:\Users\Priyanka\Documents\java> javac first.java
PS C:\Users\Priyanka\Documents\java> java first
Name of the person is: Hanuman
Name of the person Writing 2nd time: Hanuman
Name of the person is: Sita Name of the person is: Ram
PS C:\Users\Priyanka\Documents\java> javac first.java
PS C:\Users\Priyanka\Documents\java> java first
Name of the person is: Hanuman!
Name of the person Writing 2nd time: Hanuman!
Name of the person is: Sita! Name of the person is: Ram!
PS C:\Users\Priyanka\Documents\java>

 

The output of the above Code with Image:

learn how to use println and print

Note: You can also use the + sign or character to add multiple variables or variables to another variable:

 

Code Example:

public class first {


public static void main(String[] args) {
/*
         * Learn How to Use Println
         * Println prints every println in different line
         */


StringName1 = "Ram ";
StringName2 = "Sita";
StringName = Name1 + Name2;
System.out.println("Best Name in the World: " + Name);


         /*
         * Learn How to Use Print
         * Print prints every print in same line
         */


Stringname1 = "Sita!";
System.out.print("Name of the person is: " + name1);


Stringname2 = "Ram!";
System.out.print(" Name of the person is: " + name2);


    }


}

Output Result:
PS C:\Users\Priyanka\Documents\java> javac first.java
PS C:\Users\Priyanka\Documents\java> java first
Best Name in the World: Ram Sita
Name of the person is: Sita!  Name of the person is: Ram!
PS C:\Users\Priyanka\Documents\java>

 

Output with Image:

learn how to use println add two strings in java

When you use numeric values, the + character or sign works as a mathematical operator.  Notice how we have used int (integer) variables here:

 

Code Example:

public class Main {

  public static void main(String[] args) {

    int x = 41;

    int y = 71;

    System.out.println(x + y); // Print the value of x + y

  }

}

 

Output:

learn how to use println add two numbers in java

 

In the example above, you can see:

  • x stores the value 41.
  • y stores the value 71.
  • Then we use the println() method. It will display the value of x + y, which is 112.