google.com, pub-8786015629279405, DIRECT, f08c47fec0942fa0 Output Statement in Java

Output Statement in Java

0

 

Output Statement in Java
Output Statement in Java


In Java, list of the various print functions that we use to output statements:

  1. System.out.println();
  2. System.out.print();
  3. System.out.printf();

print(): This method in Java is used to display a text on the console. This text is passed as the parameter to this method in the form of String. This method prints the text on the console and the cursor remains at the end of the text at the console. The next printing takes place from just here.

Syntax

System.out.print(parameter);

 Example

// Java code to illustrate print()

import java.io.*;

 

class Demo_print {

          public static void main(String[] args)

          {

 

                   // using print()

                   // all are printed in the

                   // same line

                   System.out.print("Good");

                   System.out.print("Good ");

                   System.out.print("Good ");

          }

}

Output

Good Good Good

 

println(): This method in Java is also used to display a text on the console. It prints the text on the console and the cursor moves to the start of the next line at the console. The next printing takes place from the next line.

Syntax

System.out.println(parameter);

 Example

// Java code to illustrate println()

 

import java.io.*;

 

class Demo_print {

          public static void main(String[] args)

          {

 

                   // using println()

                   // all are printed in the

                   // different line

                   System.out.println("Good ");

                   System.out.println("Good ");

                   System.out.println("Good ");

          }

}

Output

 

Good

Good

Good

 

 

printf(): This is the easiest of all methods as this is similar to printf in C. Note that System.out.print() and System.out.println() take a single argument, but printf() may take multiple arguments. This is used to format the output in Java.

Syntax

System.out.printf( “format-string” [, arg1, arg2, … ] );

 Example

 

// A Java program to demonstrate working of printf() in Java

 

class JavaFormatter1 {

          public static void main(String args[])

          {

                   int x = 100;

                   System.out.printf(

                             "Printing simple"

                                      + " integer: x = %d\n",

                             x);

 

                   // this will print it upto

                   // 2 decimal places

                   System.out.printf(

                             "Formatted with"

                                      + " precision: PI = %.2f\n",

                             Math.PI);

 

                   float n = 5.2f;

 

                   // automatically appends zero

                   // to the rightmost part of decimal

                   System.out.printf(

                             "Formatted to "

                                      + "specific width: n = %.4f\n",

                             n);

 

                   n = 2324435.3f;

 

                   // here number is formatted from

                   // right margin and occupies a

                   // width of 20 characters

                   System.out.printf(

                             "Formatted to "

                                      + "right margin: n = %20.4f\n",

                             n);

          }

}

Output

 

Printing simple integer: x = 100

Formatted with precision: PI = 3.14

Formatted to specific width: n = 5.2000

Formatted to right margin: n =         2324435.2500

 

 

 

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Thank you for your interest 😊

We will back shortly after reviewing...

Thank you for your interest 😊

We will back shortly after reviewing...

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !
To Top