Difference between print and println in Java

When it comes to printing some output in Java, we have got some methods, like print, println, etc. In this article, we are going to discuss some points of differences between print and println methods. We would list the points of difference, and also, we would also try to have some practical examples to understand the points of differences between these methods.

So, without any further delay, let’s get started.

Difference between print and println in Java

We are going to consider some practical examples, so as to understand the points of difference between the methods print and println, but first of all, let’s have the table for points of differences here.

printlnprint
A new line is added after displaying the output.No new line is added after displaying the output.
It can also work without arguments.It cannot work without arguments.

Lets have a look at the points of differences one by one.

1. With println, after we print the output, a new line is added. On the other hand, with the print method, no new line is added.

As the point clearly says, if we are using the method println, after the output is printed to the console, a new line is going to be added. Let’s have a look at a simple program, which demonstrates the same thing.

package com.gyanipandit;
public class Example {
public static void main(String[] args) {
System.out.println(“GyaniPandit”);
}
}

As you can see, in the above program, we are calling the println method, and we are printing “GyaniPandit” to the console. If you have a look at the output of the above program, you would find that there is a new line added after printing the message.

On the other hand, if we are using the print method, then the new line will not be added. So, let’s have a look at a simple program, which demonstrates the same thing.

package com.gyanipandit;
public class Example {
public static void main(String[] args) {
System.out.print(“GyaniPandit”);
}
}

As you can see in the above program, we have called the print method instead. In this case, if you try to run the above program, and have a look at the output, you can simply find that no new line is added after printing the message. So, this is the first point of difference between the print and println methods.

2. The println method can be used without arguments, and the print method cannot be used without arguments.

When we are using the println method, we can simply use this method, without giving any arguments. Lets have a look at a simple program, which tries to demonstrate the same thing.

package com.gyanipandit;
public class Example {
public static void main(String[] args) {
System.out.println();
}
}

As you can see in the above program, we are using the println method, and in this case, we are also not passing any argument to the println method. If you try to run the above program, you won’t get any kind of error, and everything would be fine. So, we can simply say that we can use the println method without arguments.
On the other hand, if we try the same thing with the print method, the things won’t work. Lets try to have a look at a simple program, which tries to demonstrate the same thing.

package com.gyanipandit;
public class Example {
public static void main(String[] args) {
System.out.print();
}
}

As you can see in the above program, we are trying to call the print method, without arguments. In such situation, we won’t be able to run the program. So, we can simply say that the println method can be used without arguments, but we cannot use the print method without arguments.

Conclusion

In this article, we could understand the differences between the println and print methods in Java. These are some of the methods, which are used for printing some output. We have seen the differences, and also we have seen some simple examples, through which, we could understand the points of differences in a better way.

FAQ about Difference between print and println in Java

Q: Why is println method used in Java?

Ans: The println method is used to print output to the console. So, whenever we need to give output, we can make use of the println method in java.

Q: Can we use print method without argument?

Ans: No, you cannot use the print method without arguments.

Q: Can we use the println method without arguments?

Ans: Yes, you can use the println method without arguments.