(Translated by https://www.hiragana.jp/)
Difference between print() and println() in Java - GeeksforGeeks
Open In App

Difference between print() and println() in Java

Last Updated : 06 Apr, 2022
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

print(): print() 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. Various print() methods:

void print(boolean b) – Prints a boolean value. void print(char c) – Prints a character. void print(char[] s) – Prints an array of characters. void print(double d) – Prints a double-precision floating-point number. void print(float f) – Prints a floating-point number. void print(int i) – Prints an integer. void print(long l) – Prints a long integer. void print(Object obj) – Prints an object. void print(String s) – Prints a string.

Example: 

Java




import java.io.*;
 
class GFG {
    public static void main(String[] args)
    {
 
        // The cursor will remain
        // just after the 1
        System.out.print("GfG1");
 
        // This will be printed
        // just after the GfG2
        System.out.print("GfG2");
    }
}


Output:

GfG1GfG2

println(): println() method in Java is also 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 start of the next line at the console. The next printing takes place from next line. Various println() methods:

void println() – Terminates the current line by writing the line separator string. void println(boolean x) – Prints a boolean and then terminate the line. void println(char x) – Prints a character and then terminate the line. void println(char[] x) – Prints an array of characters and then terminate the line. void println(double x) – Prints a double and then terminate the line. void println(float x) – Prints a float and then terminate the line. void println(int x) – Prints an integer and then terminate the line. void println(long x) – Prints a long and then terminate the line. void println(Object x) – Prints an Object and then terminate the line. void println(String x) – Prints a String and then terminate the line.

Example: 

Java




import java.io.*;
 
class GFG {
    public static void main(String[] args)
    {
 
        // The cursor will after GFG1
        // will at the start
        // of the next line
        System.out.println("GfG1");
 
        // This will be printed at the
        // start of the next line
        System.out.println("GfG2");
    }
}


Output:

GfG1
GfG2

Difference between print() and println()

println() print()
It adds new line after the message gets displayed. It does not add any new line.
It can work without arguments. This method only works with argument, otherwise it is a syntax error.


Previous Article
Next Article

Similar Reads

Difference Between System.out.println() and System.err.println() in Java
System.out is a PrintStream to which we can write characters. It outputs the data we write to it on the Command Line Interface console/terminal. It is mostly used for console applications/programs to display the result to the user. It can be also useful in debugging small programs. Syntax: System.out.println("Your Text which you want to display");
2 min read
Difference Between System.out.print() and System.out.println() Function in Java
In Java, we have the following functions to print anything in the console. System.out.print() andSystem.out.println() But there is a slight difference between both of them, i.e. System.out.println() prints the content and switch to the next line after execution of the statement whereas System.out.print() only prints the content without switching to
2 min read
PrintWriter println(String) method in Java with Examples
The println(String) method of PrintWriter Class in Java is used to print the specified String on the stream and then break the line. This String is taken as a parameter. Syntax: public void println(String string) Parameters: This method accepts a mandatory parameter string which is the String to be printed in the Stream. Return Value: This method d
2 min read
PrintWriter println(Object) method in Java with Examples
The println(Object) method of PrintWriter Class in Java is used to print the specified Object on the stream and then break the line. This Object is taken as a parameter. Syntax: public void println(Object object) Parameters: This method accepts a mandatory parameter object which is the Object to be printed in the Stream. Return Value: This method d
2 min read
PrintWriter println() method in Java with Examples
The println() method of PrintWriter Class in Java is used to break the line in the stream. This method do not accepts any parameter or return any value. Syntax: public void println() Parameters: This method do not accepts any parameter. Return : This method do not returns any value. Below methods illustrates the working of println() method: Program
2 min read
PrintWriter println(char[]) method in Java with Examples
The println(char[]) method of PrintWriter Class in Java is used to print the specified character array on the stream and then break the line. This character array is taken as a parameter. Syntax: public void println(char[] charArray) Parameters: This method accepts a mandatory parameter charArray which is the character array to be printed in the St
2 min read
PrintStream println(float) method in Java with Examples
The println(float) method of PrintStream Class in Java is used to print the specified float value on the stream and then break the line. This float value is taken as a parameter. Syntax: public void println(float floatValue) Parameters: This method accepts a mandatory parameter floatValue which is the float value to be written on the stream. Return
2 min read
PrintStream println(long) method in Java with Examples
The println(long) method of PrintStream Class in Java is used to print the specified long value on the stream and then break the line. This long value is taken as a parameter. Syntax: public void println(long longValue) Parameters: This method accepts a mandatory parameter longValue which is the long value to be written on the stream. Return Value:
2 min read
PrintStream println() method in Java with Examples
The println() method of PrintStream Class in Java is used to break the line in the stream. This method do not accepts any parameter or return any value. Syntax: public void println() Parameters: This method do not accepts any parameter. Return : This method do not returns any value. Below methods illustrates the working of println() method: Program
2 min read
PrintStream println(double) method in Java with Examples
The println(double) method of PrintStream Class in Java is used to print the specified double value on the stream and then break the line. This double value is taken as a parameter. Syntax: public void println(double doubleValue) Parameters: This method accepts a mandatory parameter doubleValue which is the double value to be written on the stream.
2 min read
Practice Tags :
three90RightbarBannerImg