System.out.print Vs System.out.println
-
Last Updated: August 15, 2023
-
By: javahandson
-
Series
In this article, we will learn about System.out.print and System.out.println methods with some basic examples.
System.out.print(…) is used for displaying the result of a Java program on the console in the same line.
The print is the predefined instance method of PrintStream Class. It always needs a parameter.
The print is overloaded in multiple ways. Below are a few examples:
void print(boolean b) Prints a boolean value.
void print(char c) Prints a character.
void print(String s) Prints a string.
Write a program to print the characters in the same line
package com.javahandson.oops; public class Printing { public static void main(String[] args) { System.out.print("H"); System.out.print("e"); System.out.print("h"); } } Output : Heh
System.out.println(…) is used for displaying the result of a Java program on the console line by line or next line.
The println is a predefined instance method of PrintStream Class. We can use println method without parameters. If we don’t use any parameter the pointer just moves to the next line.
The println is overloaded in multiple ways. Below are a few examples:
void println(boolean x) Prints a boolean and then terminate the line.
void println(String x) Prints a String and then terminate the line.
void println() Terminates the current line by writing the line separator string.
Write a program to print the characters in the next line
package com.javahandson.oops; public class Printing { public static void main(String[] args) { System.out.println("H"); // Print and move to the next line System.out.println("e"); System.out.println("h"); } } Output : H e h
Note* We can get more information on PrintStream Class here https://docs.oracle.com/javase/8/docs/api/java/io/PrintStream.html
The println is the instance method of PrintStream Class hence to access this method we need an Object of PrintStream Class. The out is the instance name of the PrintStream Class using which we can access the println method.
System Class is a predefined Class. The PrintStream out instance is created as a static data member in the System Class.
Note* We can call the static members using the Class name.
class System { static PrintStream out = new PrintStream(); }
Hence we can access the println method like System.out.println(…)
So this is about the System.out.print and System.out.println in Java. I hope you like the article. If you are having any questions on this topic please raise them in the comments section.