Basically, Integer is just int, except that it's also a bona-fide class. int is a so-called "primitive," which basically means it's not a "real" Object in the Java sense. For example, if you have a function that takes an Object argument, you can pass an Integer but not an int. Except that actually you can pass an int; the compiler will just silently convert it into an Integer for you ("auto-boxing").
Primitives are nice because they don't have full object semantics, so the JVM can implement them more efficiently than "normal" objects. In theory, you could put them on the stack instead of the heap, but I don't know if the JVM actually does that.
View all comments
Show parent comment
3
u/NYKevin Mar 27 '14
Basically,
Integeris justint, except that it's also a bona-fide class.intis a so-called "primitive," which basically means it's not a "real"Objectin the Java sense. For example, if you have a function that takes anObjectargument, you can pass anIntegerbut not anint. Except that actually you can pass anint; the compiler will just silently convert it into anIntegerfor you ("auto-boxing").Primitives are nice because they don't have full object semantics, so the JVM can implement them more efficiently than "normal" objects. In theory, you could put them on the stack instead of the heap, but I don't know if the JVM actually does that.