Java Primitive Implementation
Asked Answered
A

2

6

Java has both object, Integer, and primitive version, int, of basic types.

The primitive versions are faster/lighter/etc. so in general you should use them.

What I am wondering is why the designers of Java didn't only have the object types and use the primitive versions as optimizations behind the scenes.

So:

Integer foo(Integer alpha)
{
    Integer total = 0;
    for(Integer counter = 0; counter < alpha; counter++)
    {
        total += counter;
    }
    return total;
}

Would be compiled into code something like:

int foo(int alpha)
{
    int total = 0;
    for(int counter = 0; counter < alpha; counter++)
    {
        total += counter;
    }
    return total;
}

Essentially, this hypothetical java compiler would convert instances of Integer, Double, Float, etc into the equivalent primitive types. Only in the cases where objects were really needed (say to put the element in a container) would actual Integer objects be involved.

NOTE: the above code has used operators on Integer objects which I know isn't actually allowed. Since I'm inventing hypothetical Java compilers I'll pretend this one has special casing for Integer/Float/Double like it does for String.

Amblygonite answered 12/3, 2011 at 0:26 Comment(4)
Not an exact duplicate, but I cover a lot of this information in #5199859Stricken
@glowcoder, I know all of that. But I'm wondering whether anything would have prevented Java from doing all of this behind the scenes and not having us worry about it. That is, we don't need to have performance concerns, because it seems straightforward for the compiler to convert uses of Long into long by itself.Amblygonite
a large issue is that your example is just simplistic. I am very glad java has primitives; they map straight to the hardware and I never saw them as obstacle. Asking for the extreme escape analysis tool is just asking too much. imagine the Integer[], how easy do you think would be to optimize, escape analyze it and generate the same code as int[] (beisides that Integer can be null). Morealso Integer is immutable and has a final field which prevents some optimizations and require even further analysis.Pollie
@bestsss, I hadn't consider the the problem of a null Integer reference, that pretty much destroys my idea. (If you put that as a question, I'll accept it as thats the sort of response I was looking for, rather then "they ran out of time") My idea idea was that you could avoid escape analysis by just converting every use of Integer to int. When assigning to Object references, then autoboxing would take place. But it occours to me know that such an attempt will not quite preserve correct object semantics.Amblygonite
T
5

What I am wondering is why the designers of Java didn't only have the object types and use the primitive versions as optimizations behind the scenes.

To understand the reasons behind the Java design decisions, you need to understand the historical context in which they were made.

The strong distinction between primitive types and reference types was baked into the language design pre JDK 1.0. Prior to JDK 1.5, that's all there was: if you wanted to put integers into collections (for example), you explicitly used Integer.valueOf(int) etc.

When auto-boxing / auto-unboxing was added to the type system in JDK 1.5, it had to be done in a way that was backwards compatible. What they came up with is a good compromise ... but not what they would / could have achieved if they had started from a clean sheet.

(And the reason that they didn't / couldn't "get it right" first time ... way back in the early 1990s is probably to do with the original language scope and the time pressures they were under to get the first release out. If they had spent extra months / years trying to get it right, the chances are that the project would have been killed ... or that the marketing window would have closed.)

Trin answered 12/3, 2011 at 1:20 Comment(1)
+1 - Because the context is king, though I'm pretty sure the language designers would've known how to do it. There are pure object languages that pre-date Java after all; from my experience most of deviations from pure OO were introduced to make life easy for people coming from C.Prosecution
K
0

You need some way to signal to the compiler that you want the boxed version, right? Otherwise, how would it know whether you wanted the version that has properties or the primitive version? What happens when you pass your Integer to a method that evaluates Integer.MAX_VALUE?

Knox answered 12/3, 2011 at 0:29 Comment(1)
Integer.MAX_VALUE is a static constant on the Integer class. No need to worry about an actual Integer object to get a hold of that one. Anytime your really need did need an Integer object, you'd autobox it.Amblygonite

© 2022 - 2024 — McMap. All rights reserved.