Preparing for Java and Spring Boot Interview?

Join my Newsletter, its FREE

Difference between int and Integer Types in Java? Example

Both int and Integer are two important data types in Java that often cause confusion between new Java developers. Both int and Integer are 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 into 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 that converts primitive into Objects.

You can also these Free Java Programming Online Courses to learn more about Java Collection Framework, very important for Java developers. 

Difference between int and Integer in Java?

How to convert int to Integer in Java? Example

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){
  // some code

Even if you are working on a Java version less than 1.5 like JDK1.4 converting an int to an 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? Example

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);

For JDK version less than 5 you can convert an Integer object into an int in Java by using the intValue() method of Integer class as shown in the 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 are easily interchangeable, there is quite a lot of difference between these two, as I have outlined below. 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. 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 a compilation error:

int number = null;

These were some notable differences between int and Integer data types 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 Example

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);
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 classes properly and avoid auto-boxing whenever possible.

Other Java and Programming Resources you may like
  • The Complete Java Developer RoadMap (roadmap)
  • 10 Things Java Programmer should learn  (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 (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 questions or doubt then please drop a note.

P. S. - If you are new to the 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.