In the recent interviews, I asked the candidates the following question:
Is there a difference that I need to consider in the following declarations? Both allocate fixed size array to store integers:
int na1 = new int; Integer na2 = new Integer;
Not to poke the feelings of passionate Java programmers … even the experienced ones seemed to have misplaced the difference. I would blame the language. Well, not the language, it is inanimate. I mean the fault lies with the language designers.
The first one
int is an array of integers, primitives. In other words, the array is going to store primitive integer values each of which are of the processor-word size. The second one
Integer is an array of objects each holding an integer inside. Each
Integer is not just equal to the size of an int. Since
Integer is inherently an
Object, there is an extra runtime/type-system associated size overhead.
Integer plays relatively hard on the GC. Along with the underlying array, each individual
Integer object becomes of the graph that the GC has to walk through during clean up.
So which array should I choose then? There isn’t one good answer. We have to make a choice. We should be able to make one based on what we discussed above. For instance, if the scenario is to operate on a fixed size collection of integers, one in which these integers would not be added to another object that takes integers only as
int would be a better choice. On the other hand, if the integers would be eventually be boxed as
Integers, for instance if we would adding them to an
ArrayList or something, then
Integer would be better; because in such a case, we would be shuffling references of the
Integer we already have in hand to the other object,
ArrayList. Why not use
ArrayList directly then? I have taken
ArrayList as an example. Like I said earlier, it is the scenario that dictates, whether to work with a fixed size collection of integers or the integers boxed in the process.
This is one of the things I wish had been done better in Java. There shouldn’t have been an
Integer at all or reference type versions of primitives. Programmers who have vowed to program only in Java (Java-only Programmers, JOPs) would find it hard to comprehend the world outside, like C# (.NET) for example.
In .NET, there are two kinds of types – reference and value. The reference types are basically classes that are allocated on the heap and the program uses a reference (handle or pointer) to access the object. Per reference semantics, the object is shared and passed across via the reference. A variable
arr1 that refers to an
ArrayList when copied to another variable
arr2 does not make a copy of the
arr2 both refer the same
ArrayList. Unlike reference types, a value type object is allocated on the stack. More importantly, it establishes the value semantics. It means when
v1 is copied to
v2, a copy of the object is made and assigned to v2. Value semantics is relatively more important and fundamental than where it is allocated in understanding value types. In C#, the so-called primitives – int, float, char etc are value types. One could define custom value types by defining a
class). Actually even the primitives are themselves
structs thus consuming the same size but augmented with the runtime/type-system facilities like being to call methods on them. I like to think of it as the one of the cornerstones of the CLR.
Unfortunately, JOPs would not have experienced the pleasure of working with value types because other than the primitives there is no way to define custom value types. JOPs would argue against the need for value types, which is why I think the candidates could not approach the question. Instead, they would argue to create a class with all
public members or other tricks to replace the notion of
structs. But they are missing the point – a class in Java does not have value semantics. An instance of a class is part of the GC graph. We are indirectly talking to the GC by allocating reference type objects. It is just the tip of the iceberg of the type system, the GC, one’s thoughtfulness in allocating objects and the way one programs.
Anyway, the intent of my question was to have an interesting discussion about the candidate’s experience with programming, in Java and beyond. And not just a question per se!