Java Output
Displaying information and results in Java programs
🖥️ Java Output Methods
Java provides several methods to display output to users. The most common is System.out.println() which prints text to the console with automatic line breaks.
// Basic output example
public class OutputExample {
public static void main(String[] args) {
System.out.println("Hello, Java World!");
System.out.println("Learning output methods");
}
}
Output:
Hello, Java World!
Learning output methods
Output Methods
println()
Print with new line
System.out.println("Hello");
System.out.println("World");
print()
Print without new line
System.out.print("Hello ");
System.out.print("World");
printf()
Formatted output
System.out.printf("Age: %d", 25);
format()
String formatting
String.format("Hello %s", "Java");
🔹 System.out.println()
The most commonly used output method:
public class PrintlnExample {
public static void main(String[] args) {
// Print different data types
System.out.println("This is a string");
System.out.println(42); // Integer
System.out.println(3.14); // Double
System.out.println(true); // Boolean
System.out.println('A'); // Character
// Print variables
String name = "John";
int age = 25;
System.out.println("Name: " + name);
System.out.println("Age: " + age);
// Print expressions
System.out.println("Sum: " + (10 + 20));
System.out.println("Is adult: " + (age >= 18));
}
}
Output:
This is a string
42
3.14
true
A
Name: John
Age: 25
Sum: 30
Is adult: true
🔹 System.out.print()
Print without automatic line breaks:
public class PrintExample {
public static void main(String[] args) {
// Print on same line
System.out.print("Hello ");
System.out.print("World ");
System.out.print("from ");
System.out.print("Java!");
// Add manual line break
System.out.println(); // Empty println for new line
// Mix print and println
System.out.print("Your score: ");
System.out.println(95);
// Print numbers in sequence
for (int i = 1; i <= 5; i++) {
System.out.print(i + " ");
}
System.out.println(); // New line after loop
}
}
Output:
Hello World from Java!
Your score: 95
1 2 3 4 5
🔹 System.out.printf()
Formatted output with placeholders:
public class PrintfExample {
public static void main(String[] args) {
String name = "Alice";
int age = 28;
double salary = 75000.50;
boolean isManager = true;
// Basic formatting
System.out.printf("Name: %s\n", name); // %s for String
System.out.printf("Age: %d\n", age); // %d for integer
System.out.printf("Salary: %.2f\n", salary); // %.2f for 2 decimal places
System.out.printf("Manager: %b\n", isManager); // %b for boolean
// Multiple values in one statement
System.out.printf("Employee: %s, Age: %d, Salary: $%.2f\n",
name, age, salary);
// Number formatting
System.out.printf("Percentage: %d%%\n", 85); // %% for literal %
System.out.printf("Padded number: %05d\n", 42); // Pad with zeros
System.out.printf("Right aligned: %10s|\n", "Hi"); // Right align in 10 spaces
System.out.printf("Left aligned: %-10s|\n", "Hi"); // Left align in 10 spaces
}
}
Output:
Name: Alice
Age: 28
Salary: 75000.50
Manager: true
Employee: Alice, Age: 28, Salary: $75000.50
Percentage: 85%
Padded number: 00042
Right aligned: Hi|
Left aligned: Hi |
🔹 String Concatenation
Combine strings and values for output:
public class ConcatenationExample {
public static void main(String[] args) {
String firstName = "John";
String lastName = "Doe";
int age = 30;
double height = 5.9;
// Using + operator
System.out.println("Full name: " + firstName + " " + lastName);
System.out.println("Age: " + age + " years old");
System.out.println("Height: " + height + " feet");
// Concatenating with calculations
System.out.println("Next year age: " + (age + 1));
System.out.println("Height in inches: " + (height * 12));
// Multiple data types
System.out.println("Info: " + firstName + " is " + age +
" years old and " + height + " feet tall");
// Using StringBuilder for efficiency (advanced)
StringBuilder sb = new StringBuilder();
sb.append("Name: ").append(firstName).append(" ").append(lastName);
System.out.println(sb.toString());
}
}
Output:
Full name: John Doe
Age: 30 years old
Height: 5.9 feet
Next year age: 31
Height in inches: 70.8
Info: John is 30 years old and 5.9 feet tall
Name: John Doe
🔹 Escape Sequences
Special characters for formatting output:
Common Escape Sequences:
- \n - New line
- \t - Tab
- \" - Double quote
- \' - Single quote
- \\ - Backslash
public class EscapeExample {
public static void main(String[] args) {
// New line
System.out.println("First line\nSecond line");
// Tab spacing
System.out.println("Name:\tJohn\nAge:\t25");
// Quotes in strings
System.out.println("He said, \"Hello World!\"");
System.out.println("It's a beautiful day");
// Backslash
System.out.println("File path: C:\\Users\\Documents\\");
// Multiple escape sequences
System.out.println("Quote:\t\"Java is awesome!\"\n\t- A programmer");
}
}
Output:
First line
Second line
Name: John
Age: 25
He said, "Hello World!"
It's a beautiful day
File path: C:\Users\Documents\
Quote: "Java is awesome!"
- A programmer