Preparing for Java and Spring Boot Interview?

Join my Newsletter, its FREE

Difference between int and Integer data type in Java? Example

The first and foremost difference between an int and Integer or a char and Character is that the former is a primitive data type while the latter is a class, also known as wrapper class because they wrap the primitive data type inside it. When you first start learning Java, you start with primitive data types like 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 the 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 in the 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 like int and wrapper classes like Integer, the main reason for that was the 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 like 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 by using static factory methods like valueOf() and then covert them back into primitive data types using toInteger() or something similar.

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

Difference between int and Integer in Java? Example

JDK 1.5 solves this problem using Autoboxing. This 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 that 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 don't know these details and that lack of knowledge sometimes causes 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;

because Java has to convert long to Long a million times, which means too many throw-away objects, which can also put pressure on the Garbage collector. You should avoid that at all costs 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. If you are new to Java, you can also check out these free Java Programming courses to learn Java fundamentals like data types, etc. 

Difference between an int and Integer in Java? Example

Another key difference between an int and an 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 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 the 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 programmers think int will be assigned default value zero, they forget to handle such kinds 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.

What is difference between an int and Integer in Java?

That's all about the difference between an int and an Integer in Java. As I told you former is a primitive data type while the latter 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 a Strong Java developer which many companies like to hire.

 Other Java articles you may like to explore

Thanks for reading this article so far. If you like this article then please share it with your friends and colleagues. 

No comments:

Post a Comment

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