Write a Java Program to Print an Integer

As a Java programmer, one of the basic tasks is to print an integer on the console.

In this tutorial, we will go through a simple Java program that does just that.


To print an integer in Java, we need to follow a few steps:

  1. Declare a variable to hold the integer value. We can use the int data type to represent an integer in Java.
  2. Assign a value to the variable. We can do this by using the assignment operator (=).
  3. Use the System.out.println() method to print the integer value on the console.

Here is a simple Java program that demonstrates how to print an integer:

public class PrintIntegerExample {
    public static void main(String[] args) {
        int number = 10; // Declare and assign a value to the variable
        System.out.println("The integer value is: " + number); // Print the integer value on the console
    }
}

In the above program, we first declared an integer variable named number and assigned a value of 10 to it.

Then we used the System.out.println() method to print the integer value on the console.

Note that we used the + operator to concatenate the string “The integer value is: ” with the integer value stored in the number variable.

This is because the println() method can only print strings, not integers.

When we run this program, we should see the following output on the console:

The integer value is: 10

That’s it! With these simple steps, we can print an integer value in Java.

This is just the tip of the iceberg when it comes to Java programming, but it is an essential first step.

With practice and experience, we can go on to write more complex programs and explore the full capabilities of the Java language.