Use int
when possible, and use Integer
when needed. Since int
is a primitive, it will be faster. Modern JVMs know how to optimize Integer
s using auto-boxing, but if you're writing performance critical code, int
is the way to go.
Take a look at this and this article. Although you shouldn't treat them as absolute truths, they do show that objects will be slower than their primitive counterparts.
So, use int
whenever possible (I will repeat myself: if you're writing performance critical code). If a method requires an Integer, use that instead.
If you don't care about performance and want to do everything in an object oriented fashion, use Integer
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…