bhylu.com

Advertisements

Java Print Text and Numbers

Output helps you observe a calculation, but the chosen print method changes how the text is arranged. Print a clear label alongside a value so a reader knows what it measures.

Understand the idea

print writes without adding a line terminator. println adds the platform line terminator after its argument. printf interprets a format string and does not add a new line unless the format requests one. A literal number participates in arithmetic; a quoted number is text. Keep formatting separate from the calculation so output choices do not accidentally change the arithmetic itself.

Run it locally

Use a local JDK 21 or newer. Save the complete example as Main.java in a separate lab folder, then compile and run it with the commands below. Java examples do not run in Bhylu’s browser playground. The -ea option enables assertion checks used by some lessons.

JDK and terminal setup

javac --release 21 -encoding UTF-8 -d out Main.java
java -ea -cp out Main

A worked example

Java 21+ · Main.java

public class Main {
    public static void main(String[] args) {
        int reserved = 7;
        System.out.print("Reserved: ");
        System.out.println(reserved);
        System.out.printf("Remaining: %d%n", 12 - reserved);
    }
}

Expected output

Reserved: 7
Remaining: 5

The first two calls share one displayed line. The final %d formats an integer and %n emits a line separator. If you replace the subtraction with a quoted expression, Java prints its characters instead of evaluating it. Console output is useful for learning, but a maintained application normally gives operational messages a deliberate logging policy.

Check the official Java documentation for detailed contracts and supported options.