Skip to content
Advertisement

Can Java primitives be considered light objects [closed]

As per this answer here

both java objects and primitives go on heap. So from the point of view of JVM, are objects and primitives similar except thaty objects take more space on the heap? In essence, are primitives nothing but ‘light’ objects?

Advertisement

Answer

Java primitives are not “light objects”. They are primitives. They fail as objects in two very significant ways: they cannot go into Collection objects and they do not have methods.

They also do not go on the heap, except as fields of an actual Java object. You cannot do new int. Note also that when you declare a local variable that is of a primitive type, the variable comes into existence. When you declare a local variable of an object type, all you get is a reference to an object, but it is set to null and no object of the declared type is allocated by simply declaring the variable.

Note that autoboxing blurs the distinction somewhat, but the distinction is definitely there.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement