Difference between print() and println() in Java

Here, We have discussed print and println() methods in Java and the differences between them:-

Difference between print() and println() in Java

print():-

print() is a method in Java that is used to show the text that the user wants to display on the screen. Whatever text has to be displayed is passed as a string to this method. The main feature of this method is that after printing the text, the cursor remains in the same line so the printing begins from the same line. If a variable having a data type is sent to this method it prints the value stored in that variable. This method works only if there is an argument.

example:-

class hello
{
   public static void main()
   {
       System.out.print("Hello, How are you"+"\t");//The cursor will remain at the same position
       System.out.print("I am fine ");
            }
}
Output:-
Hello, How are you I am fine.

println():-

println() is also used to show the text that the user wants to display on the screen. Here also the text to be displayed is passed as a string. If a variable having a data type is sent to this method it prints the value stored in that variable. The basic difference between the 2 is that after displaying the text, the cursor shifts on to the next line. This method can work even if there are no arguments.

 

class hello { 
public static void main() 
{ 
System.out.println("Hello, How are you"+"\t"); // println() will shift the cursor to the next line.
System.out.println("I am fine "); 
}
}
Output:-

Hello, How are you

I am fine.

Leave a Reply

Your email address will not be published. Required fields are marked *