Friday, March 6, 2020

Difference between int and Integer in Java?

Oracle Java Study Materials, Oracle Core Java, Oracle Java Tutorial and Material

Both int and Integer are two important data types in Java which often cause confusion between new Java developers. Both int and Integer used to represent numeric data and related to each other in the sense of primitive and Object. Int is a primitive data type that has 32-bit and stores values from  -2^31 to 2^31-1 while Integer is a class that wraps an int primitive inside it. In this article, you will learn why we need Integer Class in Java, particularly, if we already had the int data type, how to convert from int to Integer in Java, and various points on Integer in Java, ranging from basics to advanced BigInteger and AtomicInteger stuff.

Why Integer Class in Java is required?


The first question which comes in a Java trainee's mind is why we have two things int and Integer to represent the same numeric stuff. The reason for having int and Integer is that Many classes in Java work at a Class level and don't accept primitive as parameters.

The classic example is Java Collection framework classes like HashMap in Java doesn't support int primitive type as key, and you can not store int into Vector or ArrayList in Java. To facilitate these operations, Java provides a convenient wrapper class which converts primitive into Object.

How to convert int to Integer in Java?


With the introduction of autoboxing in Java, converting int to Integer in Java is trivial. You don't need to do anything special, and Java will convert an int to Integer automatically wherever requires. for example

Integer i = 30;  //valid from Java5 onwards

Even on method parameters where an Integer is expected, this will work.

public void setInteger(Integer i){}
setInteger(30);

Even if you are working on Java version less than 1.5 like JDK1.4 converting an int to Integer is easy.
see below example of converting int to Integer in JDK1.4

Integer number = new Integer(30); //int to integer in JDK1.4

How to convert Integer to int in Java?


Just like boxing, unboxing is also automatic from JDK5 onwards which makes the conversion of Integer to int in Java trivial. Compiler automatic converts an Integer object into int primitive whenever required as shown below an example of Integer to int in Java:

Integer i = 30;
int j = i; //Integer to int from Java5 onwards

public void setInt(int i);
setInt(i);

For JDK version less than 5 you can convert an Integer object into int in Java by using intValue() method of Integer class as shown in example:

Integer number = new Integer(30);
int j = number.intValue(); //Integer to int in JDK1.4

Difference between int and Integer in Java?


Though both int and Integer in Java easily interchangeable, there is quite a lot of difference between these two, as I have outlined below.

1.int is a primitive while Integer is an object in Java.

2. You can not use int in place of Integer like a key in HashMap, but with Java5, it's possible with autoboxing.

3.int is comparatively faster than Integer in Java.

4. Integer needs to be serialized and converted into bytes to be sent over RMI.

5. You can not assign null to int in Java. below would be compilation error:

    int number = null;

Integer Array vs. int array

You can create an array for both int and Integer in Java, and from Java 5 onwards, you can store Integer in place of int and vice versa as shown in the following example.

int[] intArray = {1,3,4};
Integer[] integerArray = {2, 3, 4};

you can also do like

int[0] = Integer.valueOf(5);

and

Integer[0] = 5;

Both are valid Java 5 onwards.


That's all about the difference between int and Integer in Java. Both represent integral data but there is a difference between them, int is a primitive type while Integer is a wrapper class. It's very important for a Java developer to know about this difference so that he can use both int and Integer class properly and avoid auto-boxing whenever possible.

Related Posts

0 comments:

Post a Comment