Java Output / Print Statements

Java Output / Print Statements

In Java, output is displayed on the screen using the System.out object.
There are mainly three printing methods:

Method Description
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

1️⃣ System.out.print()

This prints output on the same line.

System.out.print("Hello ");
System.out.print("Java!");

Output:

Hello Java!

2️⃣ System.out.println()

This prints output and automatically moves to the next line.

System.out.println("Hello Java!");
System.out.println("Welcome to programming.");

Output:

Hello Java!
Welcome to programming.

🆚 Difference Between print and println

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

3️⃣ System.out.printf() (Formatted Output)

Useful for formatted printing like numbers, decimals, strings.

int age = 21;
System.out.printf("Age: %d years", age);

Output:

Age: 21 years

Format Specifiers:

Format Meaning
%d Integer
%f Floating number
%.2f Floating with 2 decimal places
%s String
%c Character

Example:

String name = "Vipul";
double marks = 89.567;
System.out.printf(“Name: %s, Marks: %.2f”, name, marks);

Output:

Name: Vipul, Marks: 89.57

📌 Printing Multiple Values With Concatenation

Use + operator to combine text and variables.

String name = "Alex";
int age = 20;
System.out.println(“Name: “ + name + “, Age: “ + age);

📌 Escape Characters in Output

Escape Meaning Example Output
\n New line Moves text to next line
\t Tab space Adds extra spacing
\" Prints quotes "Hello"
\\ Prints backslash \

Example:

System.out.println("Hello\nJava");
System.out.println("Name:\tVipul");
System.out.println("He said: \"Welcome\"");

Output:

Hello
Java
Name: Vipul
He said: "Welcome"

🚀 Example Program

class OutputExample {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
System.out.print("Learning ");
System.out.print("Java Output");
System.out.println("\nFormatted Example:");
System.out.printf("Pi approx: %.3f", 3.14159);
}
}

⭐ Summary

Method Moves to new line? Use case
print() ❌ No Print continuously
println() ✔ Yes Print line-by-line
printf() ❌ No (unless you add \n) Formatted output

CodeCapsule

Sanjit Sinha — Web Developer | PHP • Laravel • CodeIgniter • MySQL • Bootstrap Founder, CodeCapsule — Student projects & practical coding guides. Email: info@codecapsule.in • Website: CodeCapsule.in

You may also like...

Leave a Reply

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