Java Print function with example | Difference between print() and println() in java

What is the difference between print() and println()?

Both the print () and println () methods print data on the screen. Basically print () and println () are almost identical with very little difference between them. These methods are very popular in Java.

Difference between print() and println() in java

In Java, the print() and println() methods differ in the way that when using println () in the output screen, the cursor will be displayed in the next line after printing the essential output on the screen. Basically, we can assume ‘LN’ as ‘next line’ in ‘println’. But in print(), the cursor in the output screen will be shown on the same line after printing the required output on the screen. This is the basic separation between print() and println() methods.

Print() method

The print () method prints the required output on the same line repeatedly on the screen. It means that it will not break the line after printing the message.

Syntax:

System.out.print(“Type your message here”);

Println() method:

The println() method prints the required output in another line repeatedly on the screen. It means that it will break the line after printing the one message.

Syntax:

System.out.println(“Type your message here”);

Java Print() Method Example:

import java.io. *;
import java.util. *;
public class Println {

public static void main(String...aa)
{
System.out.print("Hello World1. ");
System.out.print("Hello World2. ");
System.out.print("Hello World3.");
}

}

Output:

PS C:\Users\Pramod\Desktop\MPS>  c:; cd 'c:\Users\Pramod\Desktop\MPS'; & 'c:\Users\Pramod\.vscode\extensions\vscjava.vscode-java-debug-0.31.0\scripts\launcher.bat' 'C:\Program Files\Java\jdk-15.0.2\bin\java.exe' '-agentlib:jdwp=transport=dt_socket,server=n,suspend=y,address=localhost:58857' '--enable-preview' '-XX:+ShowCodeDetailsInExceptionMessages' '-Dfile.encoding=UTF-8' '-cp' 'C:\Users\Pramod\AppData\Roaming\Code\User\workspaceStorage\68adb333e7c4b36a52ea53bc8c24e1ec\redhat.java\jdt_ws\MPS_776f50d0\bin' 'Println'
Hello World1. Hello World2. Hello World3.
PS C:\Users\Pramod\Desktop\MPS>

Java Println() Method Example:

import java.io. *;
import java.util. *;
public class Println {

public static void main(String...aa)
{
System.out.println("Hello World1.");
System.out.println("Hello World2.");
System.out.print("Hello World3.");
}

}

Output:

PS C:\Users\Pramod\Desktop\MPS>  c:; cd 'c:\Users\Pramod\Desktop\MPS'; & 'c:\Users\Pramod\.vscode\extensions\vscjava.vscode-java-debug-0.31.0\scripts\launcher.bat' 'C:\Program Files\Java\jdk-15.0.2\bin\java.exe' '-agentlib:jdwp=transport=dt_socket,server=n,suspend=y,address=localhost:58896' '--enable-preview' '-XX:+ShowCodeDetailsInExceptionMessages' '-Dfile.encoding=UTF-8' '-cp' 'C:\Users\Pramod\AppData\Roaming\Code\User\workspaceStorage\68adb333e7c4b36a52ea53bc8c24e1ec\redhat.java\jdt_ws\MPS_776f50d0\bin' 'Println' 
Hello World1.
Hello World2.
Hello World3.
PS C:\Users\Pramod\Desktop\MPS>

Recommended Post:

Find the solution to the salesforce Question.
Spread the love

Leave a Reply

%d bloggers like this: