In this tutorial, you'll learn how to display output in Java using System.out.print, println, and printf. Understanding these methods is crucial for debugging and communicating with users. By the end of this lesson, you'll know how to print both text and numbers effectively.
Outputting information to the console is a fundamental aspect of programming. In Java, you can use the System.out object to display messages or data on the screen. The three primary methods for output are print, println, and printf. Each serves different purposes depending on your needs.
System.out.printThe System.out.print method prints text without moving the cursor to a new line after printing. This is useful when you want to display multiple pieces of information on the same line.
1public class PrintExample {2public static void main(String[] args) {3System.out.print("Hello");4System.out.print(" World!");5};6}
Hello World!
System.out.printlnThe System.out.println method, on the other hand, prints text and then moves the cursor to a new line. This is useful for separating different pieces of output.
1public class PrintlnExample {2public static void main(String[] args) {3System.out.println("Hello");4System.out.println("World!");5}6}
Hello World!
You can print integers and doubles using the same methods as text.
1public class NumberExample {2public static void main(String[] args) {3int number = 42;4double pi = 3.14159;56System.out.print("The answer is ");7System.out.println(number);89System.out.print("Pi is approximately ");10System.out.println(pi);11}12}
The answer is 42 Pi is approximately 3.14159
System.out.printfFor more control over the formatting of numbers, you can use System.out.printf. This method allows you to specify the format of the output using format specifiers.
1public class PrintfExample {2public static void main(String[] args) {3double pi = 3.14159;4System.out.printf("Pi is approximately %.2f%n", pi);5}6}
Pi is approximately 3.14
Here are some common format specifiers:
| Specifier | Description |
|---|---|
%d | Decimal integer |
%f | Floating-point number |
%.2f | Floating-point number with 2 decimal places |
%s | String |
%n | Newline |
Let's create a simple program that calculates the area of a rectangle and prints the result using formatted output.
1public class RectangleArea {2public static void main(String[] args) {3double width = 5.0;4double height = 10.0;5double area = width * height;67System.out.printf("The area of the rectangle with width %.2f and height %.2f is %.2f%n", width, height, area);8}9}
The area of the rectangle with width 5.00 and height 10.00 is 50.00
System.out.print to print text without moving to a new line.System.out.println to print text and move to a new line.System.out.printf for formatted output with control over the number of decimal places or other formatting options.Now that you know how to display output in Java, let's learn about adding comments to your code. Comments are essential for making your code more readable and maintainable. Move on to the next tutorial: Java Comments.