Difference between print() and println() in Java
In this tutorial, we will discuss the differences between print and println in Java language.
There are mainly two methods to display the text on the console
- println
The above two methods are of PrintStream class and are called on static member ‘out’ of ‘System’ class which is a final type class.
1. print():
The print()is the method in a java programming language to display the text from code to the console of the various platforms, and which is considered as the parameter to this method in the form of String.
The print() method displays the text on the console and the cursor remains at the end of the text rather than moving to the next line at the console. So, if we have any kind of text to display on the next line of the code, it takes place from the ending of the before line.
Example:
Suppose, We have to display “Good morning one and all “and “ Welcome to Tutorialandexample”, if we choose to print the method then the output of the code will be: Good morning one and all welcome to tutorialandexample.
Code:
import java.io. *;
class jtp{
public static void main (String args[])
{
// First Text to print
System.out.print("Good morning one and all ");
// This will print just next to the above text
System.out.print("Welcome to Tutorialandexample ");
}
}
Output:
Good morning one and allWelcome to the tutorialandexample
There are various print() methods involved in java
For Example:
- void print(int n)
it will print an integer
- void print(char ch)
it will print a character
- void print(float f)
it will print a floating point number
- void print(double db)
it will print a double-precision floating-point number
- void print(boolean bl)
it will print a boolean value
- void println(char[] x)
it will print an array of characters
- void print(long l)
it will print a long integer
- void print(Object obj)
it will print an object
- void print(String s)
it will print a string
2. println():
The println() is also the method in java to display a text from code to the console, which is considered the parameter to this method in the form of a String. This method displays the text on the console and then the cursor moves to the next line to display another text. The next text to be printed will be on the next line of the console.
Example:
Suppose, We have to display “Good morning one and all “and “ Welcome to Tutorialandexample”, if we choose to println method then the output of the code will be:
Good morning one and all
Welcome to tutorialandexample.
It means the next text will display on the console’s new line.
Code:
import java.io. *;
class jtp{
public static void main (String args[])
{
// First Text to print
System.out.print("Good morning one and all ");
// This will print just next to the above text
System.out.print("Welcome to Tutorialandexample ");
}
}
Output:
Good morning one and all
Welcome to the tutorialandexample
There are various methods involved in pritntln in java
For Example:
- void println()
the cursor moves to the next line without displaying anything on the first line
- void println(int n)
it will print an integer and then terminates to the next line of the console
- void println(char ch)
it will print a character and then terminates to the next line of the console
- void println(float f)
it will prints a floating point number and then terminate to the next line of the console
- void println(double db)
it will print a double-precision floating-point number and then terminates to the next line of the console
- void println(char[] x) –
it will prints an array of characters and then terminate the line.
- void println(boolean bl)
it will print a boolean value and then terminates to the next line of the console
- void println(long l)
it will print a long integer and then terminates to the next line of the console
- void println(Object obj)
it will prints an object and then terminates to the next line of the console
- void println(String s)
it will print a string and then terminate to the next line of the console
Difference between print() and println()
print() | println() |
It doesn’t add any newline | it adds a newline after the first message gets displayed |
Printing of the next text begins on the same line | Printing of the next text begins on the next line |
This method only works with arguments otherwise it will display a syntax error | This method can work without arguments as well |