×

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

Java InputStreamReader

What is InputStreamReader?An InputStreamReader is a converter between byte and character streams: It reads bytes and converts them to characters with the help of a charset. The charset it uses can...

4 minutes read.

Java Thread Dump Analyzer

Thread: A thread is a PC program that is stacked into the PC's memory and is under execution. It tends to be executed by a processor or a bunch of processors....

15 minutes read.

How to add double quotes in a string in Java

Strings are indicated using double-quotes. Double quotes are not printed; the values inside the double quotes are printed. There are different methods for adding double quotes to the string, such...

2 minutes read.

Java Final Keyword

In Java, the last keyword is used to limit the user. The applications of the java final keyword have large range of usage in program development. Last can be: variablemethodclass A final...

3 minutes read.

How to add 6 Months to the Current Date in Java?

In this tutorial, we will learn how to add 6 months to the local or current date in Java language. We will begin our topic with basic concepts and would...

3 minutes read.

Java Class Syntax

A class is a fundamental building piece in object-oriented programming. It can be characterised as a template that outlines the information and actions connected to the creation of a class....

3 minutes read.

Java Swing

Java Swing is the extension of Abstract Windows Toolkit (AWT). Any component designed in Swing will appear the same on any platform. Problems/Disadvantage of Abstract Windows Toolkit (AWT): As we know that...

27 minutes read.

Command Class in Java

We use the command class to run commands against the database. The Command class may find a set of parameter objects for use in sending values to a stored procedure...

3 minutes read.

Thread Concept in Java

What is a Thread? A subprocess that is lightweight in Java is known as a thread. It is the smallest unit of a process. For the running of multiple tasks parallelly...

3 minutes read.

Camel Case in Java

Java names its classes, interfaces, methods, as well as variables using camel-case syntax. If the name consists of two words, the second word will always begin with a capital letter,...

3 minutes read.

Java Single Linkedlist

Like arrays, linked lists are a type of linear data structure. Unlike arrays, which store each element in the same location, linked lists employ pointers to connect the elements together. In...

25 minutes read.

Memory Leak in Java

Java offers memory management right out of the box. When we use the new keyword to create an object, the JVM initialises for that object immediately. The trash collector automatically...

3 minutes read.

Topological Sort In Java

Topological Sort in Java Topological sort is mainly used in the linear ordering of vertices in a Directed Acyclic Graph (DAG). Topological sort in Java illustrates how to do the linear ordering of...

1 minute read.

Heap Implementation in Java

The root node or parent node of a Java heap is compared to its left and right offspring, and the children are then ordered in line with the comparison.Assuming that...

9 minutes read.

Java Plot

Java Plot is a phrase in Java that is mostly used for plotting coordinates on a cartesian plane. Plotting graphs in Java is accomplished through the use of various core...

3 minutes read.

MVC in Java

A well-known design pattern is Model-View-Controller. The discipline of web development. We can organize our code in this manner. The document stipulates that a program or application must include a...

4 minutes read.

Map of Map in Java

The map is now a Java interface for mapping keys to values. It is frequently necessary to use Map of Map (nested Map). Nested Maps are useful in various situations, including...

3 minutes read.

Java IO

It is a part of java libraries but is often known as I/O streams, file I/O, and file handling. The Java I/O concept satisfies the need for input processing and...

4 minutes read.

Java Anon Proxy

The Java Anon Proxy (JAP), also known as JonDonym, is a proxy system designed to enable Web browsing with revocable (the use of or publication under a pseudonym, a false...

4 minutes read.

Encapsulation Program in Java

Encapsulation Program in Java Encapsulation program in Java demonstrates the technique to bind methods and fields in a single unit. The term encapsulation is inspired by the word ‘capsule’, which is...

3 minutes read.