Java Output Print Statements

Java Tutorial

Java Output Print Statements

In Java Output Print Statements is displayed on the screen using the System.out object.
There are mainly three printing methods:

MethodDescription
print()Prints text but does NOT move to a new line
println()Prints text and moves to a new line
printf()Used for formatted output

 System.out.print()

This prints output on the same line.

Output:

Hello Java!

 System.out.println()

This prints output and automatically moves to the next line.

Output:

Hello Java!
Welcome to programming.

 Difference Between print and println

CodeOutput
System.out.print("Hello"); System.out.print("Java");HelloJava
System.out.println("Hello"); System.out.println("Java");Hello
Java

 System.out.printf() (Formatted Output)

Useful for formatted printing like numbers, decimals, strings.

Output:

Age: 21 years

Format Specifiers:

FormatMeaning
%dInteger
%fFloating number
%.2fFloating with 2 decimal places
%sString
%cCharacter

Example:


 

Output:

Name: Vipul, Marks: 89.57

 Printing Multiple Values With Concatenation

Use + operator to combine text and variables.


 


 Escape Characters in Output

EscapeMeaningExample Output
\nNew lineMoves text to next line
\tTab spaceAdds extra spacing
\"Prints quotes"Hello"
\\Prints backslash\

Example:

Output:

Hello
Java
Name: Vipul
He said: "Welcome"

 Example Program


 Summary

MethodMoves to new line?Use case
print() NoPrint continuously
println() YesPrint line-by-line
printf() No (unless you add \n)Formatted output

You may also like...