×

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

  1. print
  2. 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 newlineit adds a newline after the first message gets displayed
Printing of the next text begins on the same linePrinting of the next text begins on the next line 
This method only works with arguments otherwise it will display a syntax errorThis method can work without arguments as well 

Related Topics

Missing Number in an Arithmetic Progression in Java

Given an array that shows the elements of an orderly arithmetic progression. Find the missing number to complete the succession of elements. Example:  Input: a [ ] = {2 , 4 , 6...

3 minutes read.

Buffer reader to read string in Java

The Buffered Reader class of Java is used to read the stream of characters from the input stream. Program to read string using Buffer reader import java.io.*; class  Demo {   public static void main(String...

3 minutes read.

Sleep Time in Java

Sleep is amethod in java that is related to thread class and it is a concept of multithreading and is used to stop the execution of the current thread a...

4 minutes read.

Java Integer rotateRight() method

The rotateRight() method of Java Integer class returns the value obtained by rotating the  2’s complement binary representation of the given integer value right by the specified number of bits. Syntax public...

1 minute read.

Java gc()

Garbage collection Java language provides different ways to perform the task of memory management. In Java, objects are declared and assigned references. Once lifeof an object is completed it is dereferenced...

4 minutes read.

Java String replace() method

Java String replace() method returns new String by replacing old characters with new characters or old CharSequence to new CharSequence. Syntax: public String replace(char oldChar, char newChar) public String replace(CharSequence target, CharSequence replacement) Parameters: oldChar...

2 minutes read.

Java Open File

Java Desktop class give an open() strategy to open a filr. It has a place with a java.awt package. Work area execution is stage subordinate, so it is important to...

5 minutes read.

Java Binary to Hexadecimal

Converting between types in programming is an important task. Moving from one kind to another kind conversion is occasionally necessary. We have discussed numerous conversion types in the section on...

3 minutes read.

Sales Tax Problem in Java

To calculate the sales tax on a purchase in Java, you will need to know the following information: The cost of the item being purchased The sales tax rate You can then use...

4 minutes read.

Structure of Java Program

Java is well-known as an object-oriented, secure, and platform-independent programming language. The Java programming language allows us to construct a wide variety of programs. Therefore, it is essential to fully...

6 minutes read.

Interfaces and Classes in Strings in Java

CharBuffer: CharBuffer is utilized to implement the CharSequence interface. With the help of the mentioned class, we can allow character buffers to be utilized instead of CharSequences. We can consider the illustration...

4 minutes read.

Shopping Bill in Java

Java Shopping Bill Here in this program, we are about to create a JAVA class called Products which will have some properties or attributes like prod_name (Product Name ), qty (quantity),...

4 minutes read.

Java Instanceof Keyword

To determine whether an object is an instance of the supplied type in Java, use the instanceof operator (class or subclass or interface). The type of comparison in java differentiates the...

4 minutes read.

Java Integer highestOneBit()

The highestOneBit() method of Java Integer class returns an int value with at most a single one-bit, in the position of the highest-order one-bit in the specified int value.  Syntax public static...

1 minute read.

Synchronized Keyword in Java

Synchronization is the process of limiting access to a shared resource or data to a single thread at a given moment in time. This aids in shielding the data from...

6 minutes read.

Bubble Sort in Java

Bubble Sort in Java Bubble sort isalso known as sinking sort. It is one of the simplest sorting algorithms. In the bubble sort algorithm, the given array is traversed from left...

5 minutes read.

Class memory in Java

Anything specifically defined in the Java code is stored either inside the heap or the stack, which is particularly significant. Stack and heap are the two kinds of memory available...

6 minutes read.

How to get the current date and time in Java

Introduction: In this article, we are going to discover many processes for Getting the existing-day Date and Time in Java. Most programs require timestamping events or showing date/times, among many...

3 minutes read.

How to stop execution after a certain time in Java

We will learn how to stop a long-running execution after a set amount of time in this post. We will take a look at a few alternative approaches to this...

6 minutes read.

Converting Long to Date in Java

What Long and Date are in Java and how are they implemented in the Java programming language are the topics of this article. Additionally, we'll go into great detail on...

4 minutes read.