Lesson 14 of 50 – Java Output
28%

Java Output

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.

1. What Is Output in Java?

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");
    }
}
Output:
Welcome to Java!
Welcome to Soopro Pathshala
2. Using System.out.print()

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("!");
    }
}
Output:
Hello Student!
Remember: Use print() when you want multiple outputs on the same line.
3. Using System.out.println()

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.");
    }
}
Output:
My name is Rahul.
I am learning Java.
Java is interesting.

Each call to println() starts the next output on a new line.

4. Difference Between print() and println()
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");
Output:
Java Programming
Welcome
Students
5. Using System.out.printf()

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);
    }
}
Output:
Name: Amit
Age: 20

%s represents a string, %d represents an integer, and %n inserts a platform-appropriate line separator.

6. Printing Variables

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);
    }
}
Output:
Priya
19
85.5
7. Printing Text and Variables Together

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);
    }
}
Output:
My name is Rahul
My age is 21
The + operator joins the text and variable values to create the output.
8. Printing Arithmetic Results

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);
    }
}
Output:
15
5
50
2

You can also add labels to make the results easier to understand.

System.out.println("Total = " + (a + b));
Output:
Total = 15
9. Escape Sequences in Output

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");
    }
}
Output:
Hello
Students
Name:   Rahul
He said, "Hello!"
Folder: C:\Java
10. Formatting Decimal Numbers

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);
    }
}
Output:
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.

11. Common Format Specifiers
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
);
Output:
Name: Amit, Age: 20, Marks: 91.5, Grade: A, Passed: true
12. Common Output Mistakes
Mistake 1: Incorrect capitalization
// Incorrect
system.out.println("Hello");

// Correct
System.out.println("Hello");
Mistake 2: Missing quotation marks around text
// Incorrect
System.out.println(Hello);

// Correct
System.out.println("Hello");
Mistake 3: Missing semicolon
// Incorrect
System.out.println("Welcome")

// Correct
System.out.println("Welcome");
Mistake 4: Incorrect format specifier
int age = 20;

// Correct
System.out.printf("Age: %d%n", age);
Java is case-sensitive. Write System.out.println() exactly as shown, and end Java statements with a semicolon.
13. Practical Example: Student Marksheet

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);
    }
}
Output:
===== STUDENT MARKSHEET =====
Name: Aman
Roll No: 101
English: 85.50
Computer: 92.00
Total Marks: 177.50
Average: 88.75
Lesson Summary:
  • print() displays output on the same line.
  • println() moves to a new line.
  • printf() displays formatted output.
  • Escape sequences control output formatting.
  • Variables and calculations can be displayed directly.

🧠 Quick Quiz

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?