Java Program to Print an Integer

Java Program to Print an Integer

What is an Integer in Java ?

In Java, integer is a primitive data type that represents a 32-bit signed two’s complement integer. This means that an int variable can hold any whole number between -2,147,483,648 and 2,147,483,647, inclusive.
The int data type is frequently used for numeric values in Java, such as counting and indexing in loops, storing numerical values in variables, and performing arithmetic operations. It is also commonly used as the return type of methods that perform integer calculations.

Ways to Print an Integer in Java :

Syntax :

int num = 1234;
System.out.println(num);

Syntax :

int num = 1234;
System.out.printf("%d", num);

Syntax :

int num = 1234;
String numStr = String.valueOf(num);
System.out.println(numStr);

Syntax :

int num = 1234;
String numStr = Integer.toString(num);
System.out.println(numStr);

Syntax :

int num = 1234;
StringBuilder sb = new StringBuilder();
sb.append(num);
System.out.println(sb.toString());

Example 1 : 

Run

public class Main {
    public static void main(String[] args) {
    
        int i = 1234;


        // Using the System.out.println() method:
        System.out.println(i);

        // Using the System.out.printf() method with format specifiers:
        System.out.printf( "%d", i);
        

    }
}


Output :

1234
1234

Example 2 : 

Run
public class Main {
    public static void main(String[] args) {
    
        int i = 13545;
        

        // Using the String.valueOf() method to convert the integer to a string:
        System.out.println(String.valueOf(i));

        // Using the Integer.toString() method to convert the integer to a string:
        System.out.println(Integer.toString(i));

        // Using the StringBuilder class:
        System.out.println(new StringBuilder().append(i).toString());


    }
}

Output :

13545
13545
13545

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription