Output Statement in Java

  • Java
  • 4 mins read

Hello folks, in this article you will learn about the output statement in java. At the end of the article you will learn what is the System, out, and println().

In java, we generally use one of the following methods to print information on the computer’s screen.

Java provides us with the following three methods which are commonly used to print data on the output device: monitor.

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

System.out.println() Output Statement in Java

System.out.println(): It prints the data and prints a new line at the end of the output.
It can be called without passing any parameter.
Example:

System.out.println("Hello");
System.out.println("World");

The above statements print:

Hello

World

By System.out.println(), you can print the value of the variable also:

int age=20;
System.out.println(age); 

It prints 20

You can print the text and variable’s value also. To do so we use the following syntax:

int a=100;
System.out.println("The value of variable a is "+a);

The above code gives the output: The value of variable a is 100

Note: The + operator when used with string, concatenates the data with the string. So in the above println() statement "The value of variable a is "+a results in "The value of variable a is 100".

To clear the concept of string concatenation let’s take a few examples:

int a=10;
System.out.println(a+" is my favorite value"); 

The above statement prints 10 is my favorite value

int a=10,b=20;
System.out.println("Sum is "+a+b); 

The above statement prints Sum is 1020.

System.out.println("Sum is "+(a+b)); 

The above statement prints Sum is 30 because we write a+b inside the parenthesis so the addition of a and b are performed and the result is concatenated with the string.

System.out.println(); 

Using println() method without any parameter is also valid. It prints an empty line.

System.out.print() Output Statement in Java

System.out.print(): It prints data but does not change the line.
It can not be called without a parameter.

System.out.print("Hello");
System.out.print("World");

The above statement prints:

HelloWorld

System.out.print(); It is an error because the print() method does not have any definition which does not take any argument.

System.out.printf() Output Statement in Java

System.out.printf(): it works like the printf() of c language.
It uses format strings like %d, %f, etc to print the data.
It does not change the line after the output.

int a=100;
System.out.printf("%d",a);

The above statement prints 100.

Similarly, you can also print a double or float variable:

float a=12.5f;
double b=2.4;
System.out.printf("a is %f\nb is %f",a,b);

The above code prints:

a is 12.500000
b is 2.400000

Notice that %f automatically prints six digits after the decimal point. You can control how many digits you want to print after the decimal point by using . and a number between % and f inside the printf().

For example, the below printf() prints only 2 digits after the decimal point due to %.2f used in it:

float a = 12.5 f;
double b = 2.4;
System.out.printf("a is %.2f\nb is %.2f", a, b);

Output:
a is 12.50
b is 2.40

What is the meaning of System.out.println()

The System is an inbuilt class in java.

out is a predefined object of PrintStream class.

out is defined inside the System class as a static data member.

The println(), printf(), print() are library methods defined in the PrintStream class. The println() and print() methods have their overloaded forms.

So writing System.out invokes the static member out of the System class, and writing System.out.println() calls the method of out which is the PrintStream class object.