Chapter 13 of 77

Output in Java

In programming, we often need to show information to the user. For example, we may want to display a message, show the result of a calculation, or tell the user whether something went wrong.

In Java, we can display output using System.out. The two methods you will use most often are print() and println().

Using println()

The most common way to display output in Java is:

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

This prints the text Hello World! on the screen.

For example:

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

The output will be:

Hello World!
Welcome to Java!

The important thing about println() is that after displaying the value, it moves to the next line.

Using print()

Java also provides print():

System.out.print("Hello");

The main difference is that print() does not move to the next line.

For example:

System.out.print("Hello ");
System.out.print("John");

Output:

Hello John

Because both messages are printed on the same line.

Compare this with println():

System.out.println("Hello");
System.out.println("John");

Output:

Hello
John

So the easiest way to remember the difference is:

print() → stays on the same line

println() → moves to the next line

Printing Variables

We don't always want to print fixed text. We can also print the value stored inside a variable.

int age = 25;

System.out.println(age);

Output:

25

Similarly:

String name = "John";

System.out.println(name);

Output:

John

Java prints whatever value is currently stored in the variable.

Printing Text and Variables Together

We can also combine text with variables using the + operator.

String name = "John";
int age = 25;

System.out.println("Name: " + name);
System.out.println("Age: " + age);

Output:

Name: John
Age: 25

Here, the + joins the text and the value of the variable together.

We can also use it with calculations:

int age = 25;

System.out.println("Next year you will be " + (age + 1));

Output:

Next year you will be 26

The parentheses are important here because we want Java to perform the addition before joining the result with the text.

Printing Calculations

Java can also calculate values while printing them.

System.out.println(10 + 5);

Output:

15

We can use different mathematical operators as well:

System.out.println(10 - 5);
System.out.println(10 * 5);
System.out.println(10 / 5);

Output:

5
50
2

So System.out.println() isn't limited to printing text. It can also display the result of expressions and calculations.

Printing Different Data Types

We can print values of different data types using the same method.

int age = 25;
double price = 99.99;
char grade = 'A';
boolean passed = true;
String name = "John";

System.out.println(age);
System.out.println(price);
System.out.println(grade);
System.out.println(passed);
System.out.println(name);

Java will display the value of each variable according to its data type.

A Simple Example

Let's put everything together in a small program:

class Main {
    public static void main(String[] args) {

        String name = "John";
        int age = 25;
        double score = 89.5;

        System.out.println("Student Information");
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
        System.out.println("Score: " + score);
    }
}

Output:

Student Information
Name: John
Age: 25
Score: 89.5

This is how output is commonly used in Java programs to display information to the user.

Be Careful with + and Numbers

Java evaluates + from left to right. Once a String is involved, the following + operations join text instead of adding numbers:

System.out.println("Sum: " + 10 + 20);
System.out.println(10 + 20 + " is the sum");

Output:

Sum: 1020
30 is the sum

In the first line, "Sum: " + 10 becomes the text "Sum: 10", and then 20 is joined to it. In the second line, 10 + 20 is calculated first because both are numbers. Use parentheses, such as "Sum: " + (10 + 20), when you want the addition to happen first.

Escape Sequences

Some characters cannot be typed directly inside a string. For these, Java uses escape sequences, which start with a backslash \:

Escape sequence

Meaning

\n

New line

\t

Tab

\"

Double quote

\'

Single quote

\\

Backslash

For example:

System.out.println("Hello\nWorld");
System.out.println("She said \"Hi\"");

Output:

Hello
World
She said "Hi"

Formatted Output with printf()

When you want more control over how values look, for example showing exactly two decimal places, you can use System.out.printf(). It takes a format string with format specifiers followed by the values:

String name = "John";
int age = 25;
double price = 99.5;

System.out.printf("Name: %s, Age: %d%n", name, age);
System.out.printf("Price: %.2f%n", price);

Output:

Name: John, Age: 25
Price: 99.50

The common specifiers are %d for integers, %f for decimal numbers (%.2f means two digits after the decimal point), %s for strings, %c for characters, %b for booleans, and %n for a new line. Unlike println(), printf() does not move to a new line by itself, so we add %n at the end. If you need the formatted text as a String instead of printing it, use String.format() with the same format string.

The difference is simple:

System.out.print("Hello");
System.out.print("World");

Output:

HelloWorld

While:

System.out.println("Hello");
System.out.println("World");

Output:

Hello
World

So whenever you want the next output to appear on a new line, use println(). When you want to continue printing on the current line, use print().

Once you become comfortable with these two methods, displaying output in Java becomes very straightforward.