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).
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".
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.
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.
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.
}
}
You can print the value of variables and combine (concatenate) text and variables using the + operator.
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.
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.
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);
}
}
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) |
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.
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);
}
}
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.
public class Main {
public static void main(String[] args) {
System.out.println("This is normal output.");
System.err.println("This is an error message!");
}
}