Java: Print Text and Variables to the Console

Java Tutorial: Printing Text and Variables to the Console

Java Tutorial: Printing Text and Variables to the Console

This tutorial will guide you through the basics of printing text and variables to the console in Java. We’ll start with simple string printing and then move on to printing numerical values. This is a fundamental concept for debugging and understanding the flow of your programs.

Example 1: Printing a Simple String

Let’s start with the simplest case: printing a string to the console. Java uses the `System.out.println()` method for this.


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

Explanation:

  • We create a class named `PrintString`.
  • Inside the `main` method, we declare a string variable named `message` and assign it the value “Hello, World!”.
  • We use `System.out.println(message)` to print the value of the `message` variable to the console. The `println` method automatically adds a newline character at the end, so the next output will appear on a new line.

Common Beginner Mistakes:

  • Forgetting the parentheses `()` around `System.out.println()`.
  • Using a different method like `System.out.print()` without understanding the difference (the latter doesn’t add a newline).

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

Output:

> java PrintString
Hello, World!

Example 2: Printing Variable Values

Now, let’s see how to print the values of numerical variables. This is useful for debugging and verifying calculations.


public class PrintVariables {
    public static void main(String[] args) {
        int age = 30;
        double price = 99.99;
        String name = "Alice";

        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
        System.out.println("Price: " + price);
    }
}

Explanation:

  • We declare three variables of different types: `age` (integer), `price` (double), and `name` (String).
  • We print the values of these variables to the console using the `System.out.println()` method.
  • String Concatenation: Notice the use of the `+` operator to concatenate strings with the variable values. This is how you combine strings and variables when printing.

public class PrintVariables {
    public static void main(String[] args) {
        int age = 30;
        double price = 99.99;
        String name = "Alice";

        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
        System.out.println("Price: " + price);
    }
}

Output:

> java PrintVariables
Name: Alice
Age: 30
Price: 99.99

Example 3: Printing with Formatting

We can control the output format using methods like `printf`. This is particularly useful for printing numerical values with specific precision or alignment.


public class PrintFormatted {
    public static void main(String[] args) {
        double radius = 5.5;
        double area = Math.PI  radius  radius;

        System.out.printf("The area of a circle with radius %.2f is %.2fn", radius, area);
    }
}

Explanation:

  • We calculate the area of a circle using the formula `Math.PI radius radius`.
  • We use `System.out.printf()` to print the area formatted to two decimal places (`%.2f`).
  • Format Specifiers: `%.2f` is a format specifier that tells `printf` to print a floating-point number with two decimal places. `n` adds a newline character.

public class PrintFormatted {
    public static void main(String[] args) {
        double radius = 5.5;
        double area = Math.PI  radius  radius;

        System.out.printf("The area of a circle with radius %.2f is %.2fn", radius, area);
    }
}

Output:

> java PrintFormatted
The area of a circle with radius 5.50 is 94.98

Leave a Reply

Your email address will not be published. Required fields are marked *

We use cookies and similar technologies to enhance your experience on wobizdu.com, analyze site traffic, personalize content, and deliver relevant ads. Some cookies are essential for the site to function, while others help us improve performance and user experience. You may accept all cookies, decline optional ones, or customize your settings. Review our Privacy Policy to learn more.