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.