Home » System.out.println() in Java

System.out.println() in Java

by Online Tutorials Library

System.out.println() in Java

In Java, System.out.println() is a statement which prints the argument passed to it. The println() method display results on the monitor. Usually, a method is invoked by objectname.methodname().

But you cannot create the object to PrintStream class directly as above. So, Java provides an alternative way to create the object of PrintStream class that is System.out.

Where System is the class name, it is declared as final. The out is an instance of the System class and is of type PrintStream. Its access specifiers are public and final. It is an instance of java.io.PrintStream. When we call the member, a PrintStream class object creates internally.

So, we can call the print() method, as shown below:

It creates the PrintStream class object. This object, by default, represents the output device, i.e., the monitor.

System.out.println() in Java

Example

In the following example, we have used two print() methods, which gives the result in one line. It means the first print() method displays the string “Hello!” and retains the cursor at the same line. The second print() method also displays the string “Java” at the same line adjacent to the previous string.

Output

Hello! Java  

Java println() method

The println() method is similar to print() method except that it moves the cursor to the next line after printing the result. It is used when you want the result in two separate lines. It is called with “out” object.

If we want the result in two separate lines, then we should use the println() method. It is also an overloaded method of PrintStream class. It throws the cursor to the next line after displaying the result.

Example

The following example, the println() method display the string in two separate lines.

Output

Hello!   Java  

Difference between print() and println() methods

Both methods are used to display the results on the monitor. The print() method displays the result on the console and retains the cursor in the same line. It works only with an argument. The println() method also displays the result on the console but moves the cursor to the next line. It can also work without arguments.


Next TopicJava Tutorial

You may also like