- What are the differences between an Integer and an int in Java?
- Integer (Wrapper class) and int (primitive data type)
- Example1
- Output
- Example2
- Output
- Difference between an Integer and int in Java
- Example of int vs Integer
- Example
- Difference between Integer and int in Java [Practical Examples]
- Examples for Java Integer vs ins
- Example-1: Type casting from int to String
- Example-2: Builtin Methods of Integer class
- Example-3: Conversion to other base classes
- Example-4: Flexibility
- Summary
- References
- Leave a Comment Cancel reply
- Java Tutorial
What are the differences between an Integer and an int in Java?
Integer (Wrapper class) and int (primitive data type)
- The major difference between an Integer and an int is that Integer is a wrapper class whereas int is a primitive data type.
- An int is a data type that stores 32-bit signed two’s complement integer whereas an Integer is a class that wraps a primitive type int in an object.
- An Integer can be used as an argument to a method that requires an object, whereas int can be used as an argument to a method that requires an integer value, that can be used for arithmetic expression.
- An int datatype helps to store integer values in memory whereas Integer helps to convert int into an object and to convert an object into an int.
- The variable of int type is mutable unless it is marked as final and the Integer class contains one int value and is immutable.
Example1
public class PrimitiveDataTypeTest < public static void main(String []args) < // Declaration of int int a = 20; int b = 40; int result = a+b; System.out.println("Result is: " + result); >>
Output
Example2
public class WrapperClassTest < public static void main(String []args) < int a = 20; Integer b = Integer.valueOf(a); System.out.println("Converted Value of b is: " + b); Integer c = new Integer(30); int d = c.intValue(); System.out.println("Converted Value of d is: " + d); >>
Output
Converted Value of b is: 20 Converted Value of d is: 30
Difference between an Integer and int in Java
A Java both int and Integer are used to store integer type data the major difference between both is type of int is primitive while Integer is of class type.This difference become significant when concept of OOPs comes in picture during development as int follows the principle of primitive data type while Integer behave as a wrapper class.
Following are the important differences between int and Integer.
Sr. No. | Key | int | Integer |
---|---|---|---|
1 | Type | A int is a data type that stores 32 bit signed two’s compliment integer. | On other hand Integer is a wrapper class which wraps a primitive type int into an object. |
2 | Purpose | int helps in storing integer value into memory. | Integer helps in converting int into object and to convert an object into int as per requirement. |
3 | Flexibility | int provides less flexibility as compare to Integer as it only allows binary value of an integer in it. | Integer on other hand is more flexible in storing and manipulating an int data.Since Wrapper classes inherit Object class, they can be used in collections with Object reference or generics. |
4 | Memory allocation | As already mentioned int is a primitive data type and takes 32 bits(4 bytes) to store. | On other hand Integer is an object which takes 128 bits (16 bytes) to store its int value. |
5 | Casting | In java one canâTMt assign a string value (containing an integer only) to an int variable directly or even by casting. | In case of Integer we can assign string to an object of Integer type using the Integer(String) constructor or by even use parseInt(String) to convert a String literal to an int value. |
6 | Direct Conversion to Other base. | In case of int we can’t convert its integer value to other base. | However in Integer we can directly convert its integer value to other bases such as Binary, Octal or Hexadecimal format using toBinaryString(), toOctalString() or toHexString() respectively. |
7 | Allowed operations | int do not allowed any of inbuilt functions to change its value or syntax. | However in Integer we can reverse number or rotate it left or right using reverse(), rotateLeft() and rotateRight() respectively. |
Example of int vs Integer
JavaTester.java
Example
Difference between Integer and int in Java [Practical Examples]
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.
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); >>
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)); >>
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
- 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.
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)); >>
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
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.
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 Cancel reply
Java Tutorial
- Set Up Java Environment
- Set Up Java on Linux
- Set up Java with BlueJ IDE
- Set up Java with VSC IDE
- Set up Java with Eclipse IDE
- Java Multiline Comments
- Java Variables
- Java Global Variables
- Java Date & Time Format
- Different Java Data Types
- Java Booleans
- Java Strings
- Java Array
- Java Byte
- Java convert list to map
- Java convert double to string
- Java convert String to Date
- Java convert Set to List
- Java convert char to int
- Java convert long to string
- Java Operators Introduction
- Java Boolean Operators
- Java Relational Operators
- Java Arithmetic Operators
- Java Bitwise Operators
- Java Unary Operators
- Java Logical Operators
- Java XOR (^) Operator
- Java Switch Statement
- Java If Else Statement
- Java While Loop
- Java For / For Each Loop
- Java Break Continue
- Java Nested Loops
- Java throw exception
- Java Try Catch
- Java Accessor and Mutator Methods
- Java main() Method
- IndexOf() Java Method
- Java ListIterator() Method
- Java create & write to file
- Java read file
- Java Parameter
- Java Argument
- Java Optional Parameters
- Java Arguments vs Parameters
- Java Arrays.asList
- Java HashSet
- Java Math
- Java HashMap vs Hashtable vs HashSet
- Java LinkedList
- Linked List Cycle
- Java List vs LinkedList
- Java ArrayList vs LinkedList