codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
☕

Java Programming

4 / 65 topics
1Java Intro2Java Get Started3Java Syntax4Java Output5Java Comments6Java Variables7Java Data Types8Java Type Casting9Java Operators10Java Strings11Java Math12Java Booleans13Java If...Else14Java Switch15Java While Loop16Java For Loop17Java Break/Continue18Java Arrays
Tutorials/Java Programming/Java Output
☕Java Programming

Java Output

Updated 2026-05-12
15 min read

Java Output

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.

Introduction

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.

Displaying Text

Using System.out.print

The 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.

PrintExample.java
1public class PrintExample {
2 public static void main(String[] args) {
3 System.out.print("Hello");
4 System.out.print(" World!");
5 };
6}
Output
Hello World!

Using System.out.println

The 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.

PrintlnExample.java
1public class PrintlnExample {
2 public static void main(String[] args) {
3 System.out.println("Hello");
4 System.out.println("World!");
5 }
6}
Output
Hello
World!

Displaying Numbers

Printing Integers and Doubles

You can print integers and doubles using the same methods as text.

NumberExample.java
1public class NumberExample {
2 public static void main(String[] args) {
3 int number = 42;
4 double pi = 3.14159;
5
6 System.out.print("The answer is ");
7 System.out.println(number);
8
9 System.out.print("Pi is approximately ");
10 System.out.println(pi);
11 }
12}
Output
The answer is 42
Pi is approximately 3.14159

Using System.out.printf

For 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.

PrintfExample.java
1public class PrintfExample {
2 public static void main(String[] args) {
3 double pi = 3.14159;
4 System.out.printf("Pi is approximately %.2f%n", pi);
5 }
6}
Output
Pi is approximately 3.14

Format Specifiers

Here are some common format specifiers:

SpecifierDescription
%dDecimal integer
%fFloating-point number
%.2fFloating-point number with 2 decimal places
%sString
%nNewline

Practical Example

Let's create a simple program that calculates the area of a rectangle and prints the result using formatted output.

RectangleArea.java
1public class RectangleArea {
2 public static void main(String[] args) {
3 double width = 5.0;
4 double height = 10.0;
5 double area = width * height;
6
7 System.out.printf("The area of the rectangle with width %.2f and height %.2f is %.2f%n", width, height, area);
8 }
9}
Output
The area of the rectangle with width 5.00 and height 10.00 is 50.00

Summary

  • Use System.out.print to print text without moving to a new line.
  • Use System.out.println to print text and move to a new line.
  • Use System.out.printf for formatted output with control over the number of decimal places or other formatting options.

What's Next?

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.


PreviousJava SyntaxNext Java Comments

Recommended Gear

Java SyntaxJava Comments