Java Output - Complete Guide
Introduction to Java Output
Java provides several ways to display output to the console. The most common methods are through the System class, which contains the out object (PrintStream).
The primary output methods in Java are:
• System.out.print() - Prints without newline
• System.out.println() - Prints with newline
• System.out.printf() - Formatted output
• System.out.format() - Similar to printf
Basic Output Methods
The most frequently used output methods are print(), println(), and printf().
• print() writes text without appending a newline.
• println() writes text followed by a newline.
• printf() allows formatted output using placeholders.
public class OutputDemo {
public static void main(String[] args) {
// print() - doesn't add newline
System.out.print("Hello, ");
System.out.print("World!");
// println() - adds newline
System.out.println("This is on a new line");
System.out.println("And this is on another line");
// printf() - formatted output
String name = "Alice";
int age = 25;
System.out.printf("Name: %s, Age: %d%n", name, age);
}
}
Hello, World!This is on a new line And this is on another line Name: Alice, Age: 25
Escape Sequences
Escape sequences let you include special characters in strings:
• \n - Newline
• \t - Tab
• \\ - Backslash
• \" - Double quote
• \' - Single quote
public class EscapeSequences {
public static void main(String[] args) {
System.out.println("Newline: First line\nSecond line");
System.out.println("Tab: Column1\tColumn2\tColumn3");
System.out.println("Backslash: C:\\Users\\Name");
System.out.println("Double quote: She said \"Hello!\"");
System.out.println("Single quote: It\'s working");
}
}
Newline: First line Second line Tab: Column1 Column2 Column3 Backslash: C:\Users\Name Double quote: She said "Hello!" Single quote: It's working
Output Best Practices
- ✅ Use println() for user-friendly output with proper line breaks
- ✅ Use print() when building output across multiple calls
- ✅ Use printf()/format() for structured and formatted output
- ✅ Include meaningful labels in your output for clarity
- ✅ Use logging frameworks in production instead of System.out
- ✅ Test on different platforms since line endings may differ