Hacker News new | ask | show | jobs
by iceboundrock 250 days ago
Primitive variables in Java, such as `int`, `boolean`, and `double`, store their actual values directly in memory. When they are local variables inside a method, this memory is typically allocated on the thread's stack. These primitives do not have the structure or overhead of an object, including the object header used by the Garbage Collector (GC) to manage heap-allocated objects.

If a primitive value must be treated as an object (e.g., when stored in a Java Collection like ArrayList or when passed to a method that requires an object), Java uses a process called `boxing` to wrap the primitive value into an instance of its corresponding Wrapper class (e.g., Integer, Boolean, Double). These Wrapper objects are allocated on the heap and do possess the necessary object header, making them subject to the GC's management.

2 comments

Aside from this, having such a class provides a convenient place to hold all the int utility functions, and the same for the other primitive types.
Can you have a collection of primitives?
No, but yes. There is nothing in the standard library, that implements the Collection-interface and works with primitives. However you can write your own and there are several implementations of collections for primitives. The problem is that you have to implement them seperately for each type of primitive (e.g. an IntList, DoubleList, BooleanList...).
Can't you use templates for that, or do these also only work with objects?
Templates do not exist in Java.
My bad, I think they are called Generics in Java. I always thought of these as the same thing, just with different names in different languages, is that wrong?
Templates and generics are very different. Afaik Templates generate specialised code for each invocation of that template with a different type.

Java uses Type Erasure for Generics, which means that generic type variables are checked at compile time, but that type information is not available at runtime. At compile time you may have List<Foo> and List <Bar> but at runtime its all just List<Object> (Object being the base type everything inherits from). Basically List stores a bunch of pointers to objects and since pointers can point to anything a List can basically "store" anything.

Then you have a divide between primitives and Objects in Java, where primitives are double, int, boolean etc.. Primitives are types in the C-sense, e.g. int is nothing more than a 32-bit number, not a class in the Java-sense. But since ints are not classes and as such do not inherit from Object, they cannot be placed inside of List<Object>. Therefore there cannot be a List<int>. But there is Integer, which is an Object-wrapper around an int, meaning you can have a List<Integer>. This is called boxing and like all indirection carries a performance penalty.

Java hopes to heal this rift between primitives and Objects through something called Project Valhalla which has been 10 years in the making and is expected to land in the next two years or so.

Hope this ramble is comprehensible.