Java Output

Java Output: Printing to the Console

In Java, the most common way to produce output is by printing text to the console. This is incredibly useful for debugging, showing results, and interacting with the user in simple command-line applications.

The functionality is provided by the System.out object, which is part of the java.lang package (imported automatically).


1. System.out.println()

The println() method prints the specified data to the console and then moves the cursor to a new line. The "ln" in println stands for "line".

println() Example

public class Main {
  public static void main(String[] args) {
    System.out.println("Hello, Java!");
    System.out.println("This will be on a new line.");
    // You can also print numbers
    System.out.println(12345);
    // You can print mathematical operations directly
    System.out.println(500 + 50);
  }
}

If you call System.out.println() with nothing inside the parentheses, it simply prints a blank line to the console.


2. System.out.print()

The print() method is similar, but it prints the data and does not move the cursor to a new line. Any subsequent output will appear on the same line.

print() Example

public class Main {
  public static void main(String[] args) {
    System.out.print("This is the first part. ");
    System.out.print("This is the second part.");
    // Both parts will appear on the same line.
  }
}

3. Printing Variables and Concatenation

You can print the value of variables and combine (concatenate) text and variables using the + operator.

Printing Variables

public class Main {
  public static void main(String[] args) {
    String name = "John";
    int age = 30;
    System.out.println("My name is " + name + " and I am " + age + " years old.");
  }
}

When used with a String, the + operator converts other data types (like the int age) into a String and joins them together. This is a very common pattern in Java.


4. Formatted Output: System.out.printf()

Sometimes, string concatenation using the + operator can get messy, especially when you are dealing with many variables or need specific formatting (like rounding a decimal number to two places).

For these situations, Java provides System.out.printf() (print formatted). It works very similarly to the printf function in the C programming language.

You provide a format string containing text and format specifiers (which start with %), followed by the variables to fill in those specifiers.

printf() Example

public class Main {
  public static void main(String[] args) {
    String item = "Apples";
    double price = 2.49;
    int quantity = 5;
    // %s is for Strings, %d is for integers, %.2f is for floating-point with 2 decimal places
    // %n inserts a platform-independent newline
    System.out.printf("Item: %s%nPrice: $%.2f%nQuantity: %d%n", item, price, quantity);
  }
}

Common Format Specifiers

Here is a list of the most commonly used format specifiers:

Specifier Data Type Description Example Output
%s String Formats strings. "Hello"
%d Integer Formats decimal integers (byte, short, int, long). 42
%f Float/Double Formats floating-point numbers. 3.141593
%.2f Float/Double Formats floating-point numbers to 2 decimal places. 3.14
%c Char Formats a single character. 'A'
%b Boolean Formats boolean values. true
%n (none) Inserts a new line character. (new line)

Width and Alignment

You can also specify the width and alignment. For example, %10s means the string will be padded with spaces to take up at least 10 characters, right-aligned. %-10s means it will be left-aligned.

Formatting Alignment

public class Main {
  public static void main(String[] args) {
    System.out.printf("%-10s | %10s%n", "Name", "Score");
    System.out.printf("-----------------------%n");
    System.out.printf("%-10s | %10d%n", "Alice", 95);
    System.out.printf("%-10s | %10d%n", "Bob", 100);
    System.out.printf("%-10s | %10d%n", "Charlie", 88);
  }
}

5. Standard Error: System.err.println()

Java also provides a standard error stream via System.err. You use it exactly like System.out, but it is conventionally used specifically for outputting error messages and warnings.

Depending on the console or IDE you are using, text printed with System.err might appear in red to distinguish it from normal output.

Standard Error Example

public class Main {
  public static void main(String[] args) {
    System.out.println("This is normal output.");
    System.err.println("This is an error message!");
  }
}