In Java, output means displaying information on the screen.
Java provides methods such as print(),
println(), and printf() to display
text, variables, numbers, and calculated results.
Output is the information displayed by a program. For example, a program can display a student's name, marks, total, or a welcome message.
public class Main {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
System.out.println("Welcome to Soopro Pathshala");
}
}
Welcome to Java!
Welcome to Soopro Pathshala
The print() method displays information
without automatically moving to the next line.
public class Main {
public static void main(String[] args) {
System.out.print("Hello ");
System.out.print("Student");
System.out.print("!");
}
}
Hello Student!
print()
when you want multiple outputs on the same line.
The println() method displays information
and then moves to a new line.
public class Main {
public static void main(String[] args) {
System.out.println("My name is Rahul.");
System.out.println("I am learning Java.");
System.out.println("Java is interesting.");
}
}
My name is Rahul.
I am learning Java.
Java is interesting.
Each call to println() starts the next
output on a new line.
| print() | println() |
|---|---|
| Does not automatically start a new line. | Starts a new line after displaying output. |
| Useful for same-line output. | Useful for separate lines. |
System.out.print("Java ");
System.out.print("Programming");
System.out.println();
System.out.println("Welcome");
System.out.println("Students");
Java Programming
Welcome
Students
The printf() method displays formatted
output. It is useful when you want to control how
numbers and text appear.
public class Main {
public static void main(String[] args) {
String name = "Amit";
int age = 20;
System.out.printf("Name: %s%n", name);
System.out.printf("Age: %d%n", age);
}
}
Name: Amit
Age: 20
%s represents a string,
%d represents an integer, and
%n inserts a platform-appropriate
line separator.
You can display the values stored in variables using
print() or println().
public class Main {
public static void main(String[] args) {
String name = "Priya";
int age = 19;
double marks = 85.5;
System.out.println(name);
System.out.println(age);
System.out.println(marks);
}
}
Priya
19
85.5
Use the + operator to join text with
variable values in an output statement.
public class Main {
public static void main(String[] args) {
String name = "Rahul";
int age = 21;
System.out.println("My name is " + name);
System.out.println("My age is " + age);
}
}
My name is Rahul
My age is 21
+ operator joins the text and
variable values to create the output.
Java can calculate an expression and display its result directly.
public class Main {
public static void main(String[] args) {
int a = 10;
int b = 5;
System.out.println(a + b);
System.out.println(a - b);
System.out.println(a * b);
System.out.println(a / b);
}
}
15
5
50
2
You can also add labels to make the results easier to understand.
System.out.println("Total = " + (a + b));
Total = 15
Escape sequences are special character combinations used to control the formatting of output.
| Escape sequence | Meaning |
|---|---|
\n |
New line |
\t |
Horizontal tab |
\" |
Double quotation mark |
\\ |
Backslash |
\' |
Single quotation mark |
public class Main {
public static void main(String[] args) {
System.out.println("Hello\nStudents");
System.out.println("Name:\tRahul");
System.out.println("He said, \"Hello!\"");
System.out.println("Folder: C:\\Java");
}
}
Hello
Students
Name: Rahul
He said, "Hello!"
Folder: C:\Java
You can use printf() to control the number
of digits displayed after the decimal point.
public class Main {
public static void main(String[] args) {
double price = 125.5678;
System.out.printf("%.2f%n", price);
System.out.printf("%.3f%n", price);
}
}
125.57
125.568
%.2f displays a floating-point number with
two digits after the decimal point.
%.3f displays three digits after the
decimal point. The value is rounded for display;
the original variable is not changed.
| Specifier | Purpose | Example |
|---|---|---|
%s |
String | "Rahul" |
%d |
Integer | 25 |
%f |
Floating-point number | 12.5 |
%c |
Character | 'A' |
%b |
Boolean | true |
%n |
Line separator | Starts a new line |
String name = "Amit";
int age = 20;
double marks = 91.5;
char grade = 'A';
boolean passed = true;
System.out.printf(
"Name: %s, Age: %d, Marks: %.1f, Grade: %c, Passed: %b%n",
name, age, marks, grade, passed
);
Name: Amit, Age: 20, Marks: 91.5, Grade: A, Passed: true
// Incorrect
system.out.println("Hello");
// Correct
System.out.println("Hello");
// Incorrect
System.out.println(Hello);
// Correct
System.out.println("Hello");
// Incorrect
System.out.println("Welcome")
// Correct
System.out.println("Welcome");
int age = 20;
// Correct
System.out.printf("Age: %d%n", age);
System.out.println() exactly as shown,
and end Java statements with a semicolon.
This example displays student information and marks using different Java output methods.
public class Main {
public static void main(String[] args) {
String name = "Aman";
int rollNo = 101;
double english = 85.5;
double computer = 92.0;
double total = english + computer;
double average = total / 2;
System.out.println("===== STUDENT MARKSHEET =====");
System.out.println("Name: " + name);
System.out.println("Roll No: " + rollNo);
System.out.printf("English: %.2f%n", english);
System.out.printf("Computer: %.2f%n", computer);
System.out.printf("Total Marks: %.2f%n", total);
System.out.printf("Average: %.2f%n", average);
}
}
===== STUDENT MARKSHEET =====
Name: Aman
Roll No: 101
English: 85.50
Computer: 92.00
Total Marks: 177.50
Average: 88.75
print() displays output on the same line.println() moves to a new line.printf() displays formatted output.Q1. Which method automatically moves to a new line?
Q2. Which format specifier is used for an integer in printf()?
Q3. Which escape sequence represents a tab?