Wednesday, 14 February 2018

What is difference between an int and Integer in Java?

The first and foremost difference between an int and Integer or a char and Character is that former is a primitive data type while later is a class, also known as wrapper class because they wrap primitive data type inside it. When you first start learning Java, you start with primitive data types e.g. int, long, char, byte, boolean, float, and double but slowly you learn about Object and sometime later you know about Integer, Long, Character, Byte, Boolean, Float, and Double. At this point in time, you may wonder, what is the real difference between an int and Integer? isn't both same? We can pass Integer where int is expected and vice-versa then why on the earth we have both int and Integer?

Well, you are thinking on right direction because auto-boxing has blurred the difference between an int and Integer in Java but if you want to learn Java well, especially core Java, you need to understand fundamentals like this. Let me tell you a story.

When Java first come in it has both primitive types e.g. int and wrapper classes e.g. Integer, the main reason for that was Collection framework. It wasn't possible until JDK 1.5 to store an int into a List, Set or Map, that's why you need to first convert int to Integer and then store them into any Collection classes e.g. ArrayList, HashSet, or HashMap.

Since Collection classes if often indispensable for any Java application, people need to work their way with wrapper classes. They often need to convert int to Integer e.g. by using static factory methods like valueOf() and then covert them back into primitive data type using toInteger() or something similar.

It was also not possible to pass an int to a method which is expecting an Integer, so you need to wrap the primitive into a wrapper object e.g. int into Integer or char into Character.

How Java Solves this Problem?


JDK 1.5 solves this problem using Autoboxing. Which means you don't need to convert an int to Integer or float to Float or char to Character, Java runtime will automatically do that for you.

That's why now it's possible to pass an int to a method which is expecting an Integer or assign the int to an Integer reference variable or vice-versa.

Many Java programmers who have started after JDK 1.5 doesn't know this details and that lack of knowledge sometimes cause them in terms of performance issues or NullPointerException.

For example, if you have read Effective Java then you know that you should never do autoboxing in the loop, it affects the performance big time. For example, a code like this will perform poorly:

for(long i=0L; i< 1000000; i++){
  Long number = i;
  System.out.println(number);
}

because Java has to convert long to Long a million times, which means too many throw-away objects, which can also put pressure on Garbage collector. You should avoid that at all cost and if you have read that item from Effective Java. I strongly suggest you read it, also a newer version of Effective Java is now available, Effective Java 3rd Edition which covers JDK 7, 8, and 9 enhancements.

Another key difference between an int and Integer is that int can never be null but Integer can be. That's obvious if you know the difference between primitive type and Wrapper class but what you may not know that this can cause NullPointerException in your code.

For example, consider this code:

Map<String, Integer> wordCount = new HashMap<String, Integer>();
int count = wordCount.get("Java"););

This code can throw NullPointerException if the wordCount map doesn't contain any word or key "Java". This happens because get()  method will return null if the key is not present in the map and because of autoboxing Java will try to convert null to an int value.

Since Java programmer think int will be assigned default value zero, they forget to handle such kind of scenarios, unless they know about it.

That's why it's important that you know Java well and true and if you are serious about learning Java in depth then The Complete Java MasterClass on Udemy is a good place to start with.

Oracle Java Tutorials and Materials, Oracle Java Certifications, Oracle Java Learning

That's all about the difference between an int and Integer in Java. As I told you former is a primitive data type while later is a wrapper class. This also means that int can never be null but Integer can be null and can throw NullPointerException if you use auto-boxing to convert Integer to int or vice-versa.

Even though Auto-boxing has reduced the gap between an int and Integer or say between any primitive data type to the wrapper object, it's still important to know what they are how they work. Especially, if you want to become that Strong Java developer which many companies like to hire.

Related Posts