← Back to all posts

06 Java Fundamentals

Output in Java

By Dummy for Dummies


Output in Java

Alright, fellow monkeys, we've learned how to take input. Now let's talk about giving output back. You already know print from earlier:

Java
System.out.println("Hello, World!");

That same line handles every kind of output, just with small changes depending on what you need. Here they are, with the golden lines in focus:


1. Simple Output

Java
public class Main {
    public static void main(String[] args) {
        System.out.print("Hello, World!");
    }
}
Console Output
Hello, World!

Another way to print it is:

Java
public class Main {
    public static void main(String[] args) {
        System.out.print("Hello");
        System.out.print("World");
    }
}
Console Output
HelloWorld

Therefore, we need a way to print content across different lines for better readability.


2. Output with Next Line

Java
public class Main {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}
Console Output
Hello, World!

3. Output with Variable

Java
public class Main {
    public static void main(String[] args) {
        int age = 20;
        System.out.println("Your age: " + age);
    }
}
Console Output
Your age: 20

4. Formatted Output

Formatted output lets you print things in a specific layout. Concatenation with + doesn't work here. Here's a demonstration:

Java
public class Main {
    public static void main(String[] args) {
        int age = 20;
        System.out.printf("Your age: %d", age);
    }
}
Console Output
Your age: 20

Placeholders for Each Datatype

PlaceholderDatatype
%dInteger
%fDouble / Float
%cCharacter
%sString
%bBoolean

Extra Things for Styling


Complete Example

Java
public class OutputDemo {
    public static void main(String[] args) {
        int age = 20;
        double gpa = 3.75;
        char grade = 'A';
        String name = "Hassan";
        boolean pass = true;

        System.out.printf("Student Details:\n");
        System.out.printf("Name:\t%s\n", name);
        System.out.printf("Age:\t%d\n", age);
        System.out.printf("GPA:\t%.2f\n", gpa);
        System.out.printf("Grade:\t%c\n", grade);
        System.out.printf("Passed:\t%b\n", pass);

        System.out.printf("Quote Example:\t\"Work hard, dream big!\"\n");
        System.out.printf("Backslash Example:\tC:\\Users\\Hassan\n");
    }
}
Console Output
Student Details: Name: Hassan Age: 20 GPA: 3.75 Grade: A Passed: true Quote Example: "Work hard, dream big!" Backslash Example: C:\Users\Hassan

Exercise

We have covered enough topics to go for an exercise. In this exercise I want you to study this code, break it down, analyze it and identify the concepts used here. It would be even better if you write down your observations and make a mini report on it. You can extend the program to accept a second student and print both reports.

Java
import java.util.Scanner;

public class Exercise {
    public static void main(String[] args) {
        System.out.println("Welcome to Java Fundamentals Exercise!");
        System.out.println("Let's practice everything we have learned so far.\n");

        String name;
        int age;
        double gpa;
        char grade;
        boolean pass;

        Scanner input = new Scanner(System.in);

        System.out.print("Enter your name: ");
        name = input.nextLine();

        System.out.print("Enter your age: ");
        age = input.nextInt();

        System.out.print("Enter your GPA: ");
        gpa = input.nextDouble();

        System.out.print("Enter your grade (A-F): ");
        grade = input.next().charAt(0);

        System.out.print("Did you pass the last exam? (true/false): ");
        pass = input.nextBoolean();

        System.out.println("\n    GENERATING REPORT\n");

        int nextYearAge = age + 1;
        boolean isAdult = age >= 18;
        boolean eligible = isAdult && pass;

        System.out.printf("Student Report for %s\n", name);
        System.out.printf("Age: %d (Next year: %d)\n", age, nextYearAge);
        System.out.printf("GPA: %.2f\n", gpa);
        System.out.printf("Grade: %c\n", grade);
        System.out.printf("Passed: %b\n", pass);
        System.out.printf("Are you an adult? %b\n", isAdult);
        System.out.printf("Eligible for next level? %b\n", eligible);

        input.close();
    }
}
Console Output
Welcome to Java Fundamentals Exercise! Let's practice everything we have learned so far. Enter your name: Hassan Enter your age: 20 Enter your GPA: 3.75 Enter your grade (A-F): A Did you pass the last exam? (true/false): true GENERATING REPORT Student Report for Hassan Age: 20 (Next year: 21) GPA: 3.75 Grade: A Passed: true Are you an adult? true Eligible for next level? true

Mini Project: Student Report Card

Build a Java program that works like a student report card. It should:

Finally, print a formatted report using printf. The report must include:

While writing the program, use a Scanner method for each data type, plus arithmetic, relational, and logical operators, and printf with placeholders like %s, %d, %.2f.


Example Output

Console Output
STUDENT REPORT CARD Name: Tayyab Subject 1: 50 Subject 2: 60 Subject 3: 70 Total Marks: 180 Average: 60.00 Result: PASS

Closing

That's it for Output in Java! We now know how to make our program talk back, whether it's a single word, a full sentence, or a nicely formatted report.

← Previous 05 - Input in Java Next → 07 - If-else statements in Java