Int vs Integer in Java
In Java, numerical data can be stored using int or Integer. int and Integer both have different definitions but similar usage in Java.
What is int in Java?
- int is a primitive data type in Java. It can be used to store and manipulate integer values.
- It stores the data values using a 32-bit signed two’s complement integer.
- The value stored in data type int is changeable unless it is declared as final by the developer.
The following Java program demonstrates the use of int data type in Java.
Demoint.java
public class Demoint
{
/* Driver Code */
public static void main(String []args)
{
/* int variables declaration */
int a = 5, b = 8, c;
System.out.println("int value 1: " + a +"\nint value 2: " + b);
/* Addition operation */
c = a + b;
System.out.println("Addition of two int variables = " + c);
}
}
Output:
int value 1: 5
int value 2: 8
Addition of two int variables = 13
In the above code, two int variables, a and b are declared and the addition is stored in the variable c.
What is Integer in Java?
- Integer is a special class in Java also called wrapper class. It wraps the value stored as an int primitive data type into an object.
- The need of the Integer class is because Java is object-oriented and many classes don’t accept primitive data type as argument. For example, HashMap in Java accepts key as an Integer object.
- Integer class provides several methods for converting an int to String or vice versa.
The following Java program demonstrates the use of the Integer class in Java.
DemoInteger.java
public class DemoInteger
{
/* Driver Code */
public static void main(String []args)
{
/* Integer object creation */
Integer a = new Integer("9");
Integer b = new Integer("15");
Integer c;
System.out.println("Integer value 1: " + a +"\nInteger value 2: " + b);
/* Addition operation */
c = a + b;
System.out.println("Addition of two Integer objects = " + c);
}
}
Output:
Integer value 1: 9
Integer value 2: 15
Addition of two Integer objects = 24
In the above code, two Integer class objects, a and b are declared and the addition is stored in the object c.
Other wrapper classes for primitive data types in Java
Primitive data type | Wrapper Class |
char | Character |
byte | Byte |
short | Short |
long | Integer |
float | Float |
double | Double |
boolean | Boolean |
The following Java program demonstrates a few methods of Integer class in Java.
IntegerMethods.java
public class IntegerMethods {
public static void main(String args[])
{
String bin = Integer.toBinaryString(123);
String oct = Integer.toOctalString(123);
String hex = Integer.toHexString(123);
System.out.print(bin + "\n" + oct + "\n" + hex);
}
}
Output:
int: 123
binary: 1111011
octal: 173
hex: 7b
The above code shows the use of Integer class methods like parseInt(), toBinaryString(), toOctalString(), toHexString().
int vs Integer
Sr. No. | int | Integer |
1. | The primitive data type int can store 32-bit signed two’s complement integer value. | The Integer is a wrapper class in Java that allows to store int data type value as an Integer object. |
2. | It stores integer value into memory. | It helps to convert int value as Integer object and vice versa. |
3. | Since an int allows only the binary value of an integer to be stored, it is less flexible. | The Integer class is more flexible as it inherits properties from Object class |
4. | int data type takes 32-bits (4 bytes) to store a numeric value. | The Integer class takes 128-bits (16 bytes) to store the integer object. |
5. | int data type doesn’t provide casting functionality for decimal or String type values. | The Integer class allows the casting of decimal or String type values. Integer(String) or parseInt(String) methods can be used for casting a string to integer data. |
6. | Integer value stored using int data type cannot be directly converted into other base values. | Integer class provides methods like toBinaryString(), toOctalString(), toHexString() for converting integer value to other base values. |
7. | Using int data type we cannot perform any operation because it doesn’t provide any built-in functions. | Integer class provides multiple methods like reverse(), rotateLeft(), rotateRight() for performing different operations on integer value. |
Why Java provides int data type and Integer class separately?
Primitive data type int provides limited functionalities for data values stored using it. The Integer class stores the data values as objects. Many operations can be performed on it using the built-in methods of the Integer class.
This article discusses the basics and difference between primitive data type int and Integer class in Java.