Java Output/Print 🎯

beginner
22 min

Java Output/Print 🎯

Welcome to the Java Output/Print tutorial! In this comprehensive guide, we'll learn how to print text and display output in Java. By the end of this lesson, you'll be able to create engaging and interactive applications. Let's get started! 📝

Understanding Output 💡

In programming, output refers to the data or results that a program generates during its execution. In Java, we use the System.out.println() method to display output.

The System.out.println() Method 💡

The System.out.println() method belongs to the PrintStream class, which is associated with the standard output stream (System.out). This method prints its argument and advances the cursor to a new line.

java
System.out.println("Hello, World!");

Try it Out!

java
public class Main { public static void main(String[] args) { System.out.println("Hello, World!"); } }

What's happening here?

  1. The main method is the entry point for every Java application.
  2. System.out is the standard output stream.
  3. println() is a method that prints its argument and moves the cursor to a new line.
  4. "Hello, World!" is the text we want to print.

Printing Variables 💡

You can print variables using the println() method by concatenating them with strings.

java
int myNumber = 10; System.out.println("The number is " + myNumber);

Try it Out!

java
public class Variables { public static void main(String[] args) { int myNumber = 10; System.out.println("The number is " + myNumber); } }

Formatting Output 💡

Java provides several methods to format output, such as printf(). This method can be used to print formatted text, including numbers, with placeholders.

java
double price = 9.99; System.out.printf("The price is: $%,.2f", price);

Try it Out!

java
public class Formatting { public static void main(String[] args) { double price = 9.99; System.out.printf("The price is: $%,.2f", price); } }

Quiz

Quick Quiz
Question 1 of 1

Which method is used to display output in Java?

Summary 📝

In this tutorial, we covered the basics of output and printing in Java. We learned how to print text using the System.out.println() method, print variables, and format output using the printf() method. By the end of this lesson, you should be able to create simple and interactive Java applications. Keep practicing, and happy coding! 💡🎯

Quiz

Quick Quiz
Question 1 of 1

What does the `System.out.println()` method do?