Difference between int and Integer in Java?

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.

You can learn more about the Java Collection framework on Java Fundamentals: Collections course on Pluralsight by Java Champion Richart Warburton.

Difference between int and Integer in Java?



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;

These were some notable difference between int and Integer data type in Java. You can learn more about these data types in The Complete Java Masterclass, one of the best and most up-to-date courses to learn Java.

Integer Array vs. int array


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.

Further Learning
The Complete Java Masterclass
Java Fundamentals: The Java Language
Learn Object-Oriented Programming in Java


Other Java and Programming Resources you may like

  • The Complete Java Developer RoadMap (roadmap)
  • 10 Things Java Programmer should learn in 2020 (things)
  • 10 Books Every Programmer Must Read (books)
  • 10 Courses to learn DevOps in Depth (courses)
  • 10 Tips to Improve Your Programming skill (tips)
  • 10 Tools Every Software Developer should know (tools)
  • 5 Courses to Learn Software Architecture in Depth (courses)
  • 20 Libraries and APIS Java Programmer Should Know (libraries)
  • Top 10 Programming languages to Learn in 2020 (languages)
  • 10 Articles Every Programmer Should Read (articles)
  • 10 Framework and Library Java and Web Developer Should Learn (frameworks)
  • Top 5 Courses to Learn Microservice in Java (course)

Thanks for reading this article so far. If you find this article useful and able to understand essential OOP concepts like overloading, overriding, hiding, shadowing and obscuring them please share with your friends and colleagues on Facebook, Twitter, or Linkedin. If you have any question or doubt then please drop a note.


P. S. - If you are new into Java Programming world and want to learn Java but looking for a free course then you can also check out this list of 10 Free Java Programming courses for beginners and anyone who wants to learn Java. 

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.