Difference between Integer and int in Java [Practical Examples]

Differentiate between Integer and int in Java

In Java, int is a primitive data type whereas Integer is a Wrapper class. In other words, int can store the values in the range of -2^31 to 2^31-1. However, Integer is a class that wraps an int primitive inside it. Primitive type int needs 4 bytes in Java but, Integer object occupies 16 bytes of memory. Hence, int is comparatively faster than an Integer object.

Since, int is a primitive type it is less flexible. We can only store the binary value of an integer in it. However, the Integer class wraps the int data type in it. So we can store, convert, and manipulate the int data with more flexibility.

Advertisement

With an Integer class, we can call various built-in methods defined in the class. Integer objects can then be passed to other class objects. Collections in java do not support parameters of primitive data type. So, in this case we have to convert primitive data type variable to its corresponding object.

The table below summarize the key difference between Integer and int in Java.

Description int Integer
Type Primitive Data type Integer Wrapper class
Memory Usage 4 bytes 16 bytes
Type casting to String Not Possible Possible
Conversion to other base class Not Possible Possible
Can use built-in methods No Yes
Provides Flexibility No Yes

Let us now see in detail the difference between Integer and int in Java along with an example.

 

Examples for Java Integer vs ins

Example-1: Type casting from int to String

The difference between Integer and int is we cannot directly type cast an int variable to a String object. However, this can be done by making use of Integer wrapper class. The parseInt method of the Integer wrapper class is used to typecast a int value to a String object.

Example : The example below shows the difference between Integer and int in Java. The direct type casting from a object to primitive type is not supported in Java. However, we can do this with the help of Integer class.


// Program demonstrates the difference between Integer and int in Java
public class Main {
    public static void main(String args[]) {
        String s = "100";

        // Direct type Casting from object to primitive type is not possible in Java
        // int a = (int)s;
        // Type Casting is possible using Integer Wrapper class
        int i = Integer.parseInt(s);
        System.out.print("Value of i is " + i);
    }
}

Output :

Advertisement
Value of i is 100

 

Example-2: Builtin Methods of Integer class

In Java, the key difference between Integer and int is that primitive type does not support any built-in methods. Whereas, Integer class supports various built-in methods like sum, reverse, min, max, decode, rotateLeft, rotateRight etc. However, if we want to perform this operations with int type variable, we have to write a user defined function to perform each of this task.

Here is the list of some built-in methods supported by an Integer class.

  • sum - Returns addition of two numbers passed in the parameter.
  • min - Returns minimum of two numbers passed in the parameter.
  • max - Returns maximum of two numbers passed in the parameter.
  • reverse - Returns the value obtained by reversing the order of the bits in the two's complement binary representation of the specified int value.
  • decode - Decodes a String into an Integer. It converts the given hex or octal number to equivalent decimal number.
  • rotateLeft - Returns the value obtained by rotating the two's complement binary representation of the specified int value left by the specified number of bits.
  • rotateRight - Returns the value obtained by rotating the two's complement binary representation of the specified int value right by the specified number of bits.

 

Example : The example below shows the difference between Integer and int in Java. Here, we will use built-in methods of an Integer wrapper class.

// Program demonstrates the difference between Integer and int in Java
public class Main {
    public static void main(String[] args) {
        int i = 10, j = 20;
        String s = "0x1225";

        // Finding sum of two numbers
        System.out.println("Summation of " + i + " and " + j + " is " + Integer.sum(i, j));

        // Finding Maximum of two numbers
        System.out.println("Maximum of " + i + " and " + j + " is " + Integer.max(i, j));

        // Finding Minimum of two numbers
        System.out.println("Minimum of " + i + " and " + j + " is " + Integer.min(i, j));

        // Converting Hexadecimal to Integer
        System.out.println("Decimal equivalent of " + s + " is " + Integer.decode(s));

        // Binary equivalent of 10 is 0000 0000 0000 0000 0000 0000 0000 1010
        // Reverse is 0101 0000 0000 0000 0000 0000 0000 0000
        // 2^30 + 2^28 = 1342177280 in decimal
        System.out.println("Reverse of " + i + " is " + Integer.reverse(i));

        // Binary equivalent of 10 is 0000 0000 0000 0000 0000 0000 0000 1010
        // After rotate right by 2 postion 0100 0000 0000 0000 0000 0000 0000 0010
        // -2^31 + 2^1 = -2147483646 in decimal
        System.out.println("Rotate all bits of " + i + " to the right by 2 position : " + Integer.rotateRight(i, 2));

        // Binary equivalent of 10 is 0000 0000 0000 0000 0000 0000 0000 1010
        // After rotate left by 2 postion - 0000 0000 0000 0000 0000 0000 0010 1000
        // 2^5 + 2^3 = 40 in decimal
        System.out.println("Rotate all bits of " + i + " to the left by 2 position : " + Integer.rotateLeft(i, 2));

    }
}

Output

Summation of 10 and 20 is 30
Maximum of 10 and 20 is 20
Minimum of 10 and 20 is 10
Decimal equivalent of 0x1225 is 4645
Reverse of 10 is 1342177280
Rotate all bits of 10 to the right by 2 postion : -2147483646
Rotate all bits of 10 to the left by 2 position : 40

 

Example-3: Conversion to other base classes

In Java, Integer class supports various built-in methods to convert the integer value stored as a string into an equivalent binary, octal and hexadecimal number. However, if we want to perform this conversion with int type variable, we have to write a user defined function to perform each of this task.

Here is the list of some built-in methods supported by an Integer class

Advertisement
  • toBinaryString - Returns a string representation of the integer argument as an unsigned integer in base 2.
  • toHexString - Returns a string representation of the integer argument as an unsigned integer in base 16.
  • toOctalString - Returns a string representation of the integer argument as an unsigned integer in base 8.

 

Example : The example below shows the difference between Integer and int in Java. Here, we will use methods of Integer class for conversion to different number systems.


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

        // Converting int to Binary
        System.out.println("Binary equivalent of " + i + " is " + Integer.toBinaryString(i));

        // Converting int to Octal
        System.out.println("Octal equivalent of " + i + " is " + Integer.toOctalString(i));

        // Converting int to Hexadecimal
        System.out.println("Hexadecimal equivalent of " + i + " is " + Integer.toHexString(i));
    }
}

Output

Binary equivalent of 100 is 1100100
Octal equivalent of 100 is 144
Hexadecimal equivalent of 100 is 64

 

Example-4: Flexibility

Integer wrapper class is more flexible as compared to primitive int datatype. When int variable is used as Integer object, it broadens the access to other features of Java. Integer Wrapper class inherit Object class, so we can use it with collections as a Object reference. The variable of int type cannot accept NULL value, but if it is used as Integer object it can accept the NULL value. Thus, this feature adds the property of nullability to the primitive int data type.

From Java 5 onwards, there is an automatic type conversion from data type to wrapper class which is referred as auto-boxing. Thereby, this feature allows us to perform any arithmetic or logical operation between a primitive data type and a Wrapper class.

Example : The example below shows the difference between Integer and int in Java.

// Program demonstrates the difference between Integer and int in Java
public class Main {
    public static void main(String[] args) {
        Integer r = new Integer("100");
        int x = 2;
        float pi = 3.14159 f;
        Double f = new Double("15.45");

        // Adding Integer object with double object
        System.out.println("Sum of Integer objects with Double object: " + (r + f));

        // Computing Area of circle using Integer object with float value
        System.out.println("Computing area of circle with Integer object and float value " + (pi * r * r));

        // Computing Area of Rectangle using Integer object and int value
        System.out.println("Computing area of rectangle with Integer object and int value " + (r * x));

    }
}

Output

Advertisement
Sum of Integer objects with Double object: 115.45
Computing area of circle with Integer object and float value 31415.9
Computing area of rectangle with Integer object and int value 200

 

Summary

The knowledge of difference between Integer and int is very important for a Java developer to know so that he can use both int and Integer classes properly and avoid auto-boxing whenever possible. In this tutorial, we covered the key difference between Integer and int in Java. We learned in detail about the usage of Integer built-in methods to ease the small task with example. All in all, this tutorial, covers everything that you need to know in order to understand the difference between Integer and int in Java and select an appropriate type as per the requirement of an application.

 

References

Integer class

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment

X