Java int or integer


In Java, should I use “int” or “Integer”?

This is common question for new Java developer like me. A lot of discussion can be found through google with searching on this question. Let me be clear, the difference between int and Integer can be seen from their parent.

As we know, int is primitive data-type in Java, meaning it will have same position as byte, short, float, etc. Meanwhile, Integer is a wrapper.

Yes, since Integer is not primitive as int, theoretically it’s slower than int. But, that’s not the point of this question.

1. Do you want to store NULL ?
If you want to store Null into database (Remember, not all database support NULL and it’s different from 0), then you should use “int”.

2. No! I want only store 0 even if it’s NULL or empty
Then, you should go to Integer since it’s have “Autoboxing” and “Unboxing” features. Detail : http://docs.oracle.com/javase/tutorial/java/data/autoboxing.html

Based on this two things, you can make consideration, which one you would use.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.