Java From Beginner To Advanced

0% completed

Previous
Next
Java Basics of Output

In Java, the System class provides access to the system, including input and output through standard streams. In particular, the out object (an instance of the PrintStream class) is used for displaying output on the console. Java offers several methods to print text on the console:

  • System.out.println(); – Prints text with a new line at the end.
  • System.out.print(); – Prints text without adding a new line.
  • System.out.printf(); – Prints formatted text using format specifiers.

Below, we explain these methods and show examples that print variables and concatenated strings.

1. System.out.println();

This method prints a message to the console and then moves the cursor to a new line.

Example: Using System.out.println()

Here, we declare an integer variable num and a string variable text. Using System.out.println(), we print a concatenated string that includes the variable values. After printing, the cursor moves to a new line for the next output.

Java
Java

. . . .

2. System.out.print();

This method prints text without moving the cursor to a new line. It is useful when you want to continue printing on the same line.

Example: Using System.out.print()

Here, we declare two string variables, firstPart and secondPart. By using System.out.print(), we output both strings consecutively on the same line without inserting a new line between them.

Java
Java

. . . .

3. System.out.printf();

The System.out.printf() method allows you to print formatted text. This method is particularly useful for controlling how numbers and text appear in the output. You can use format specifiers to define the output format, such as specifying the number of decimal places or aligning text.

Commonly Used Format Specifiers

The table below shows some of the commonly used format specifiers:

SpecifierDescriptionExample
%dPrints a decimal (integer) numberSystem.out.printf("%d", 123); outputs 123
%fPrints a floating-point numberSystem.out.printf("%.2f", 12.3456); outputs 12.35
%sPrints a stringSystem.out.printf("%s", "Hello"); outputs Hello
%cPrints a single characterSystem.out.printf("%c", 'A'); outputs A
%nInserts a platform-independent newlineSystem.out.printf("%n"); outputs a newline
%bPrints a boolean valueSystem.out.printf("%b", true); outputs true

Example: Using System.out.printf()

Java
Java

. . . .

Explanation:

  1. Floating-Point Formatting: We use %.2f to round the piValue to 2 decimal places.
  2. Integer Formatting: We use %d to print the integer count.
  3. String Formatting: We use %s to include the message variable.
  4. Multiple Specifiers: We combine %f, %d, and %s in a single statement to print all the variables together in a formatted manner.

These methods form the basis of console output in Java, enabling you to display data, variables, and even formatted messages efficiently. Experiment with these examples in your code editor to see how each method works and how you can combine them with variables and concatenated strings.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next