Dale said:
If Java primitives
were objects just like the current objects that would be a nightmare
performance and understanding wise. The real issue here is value
semantics vs. reference semantics.
I don't want to sound hostile, but this is completely wrong.
Take a look at a typical Smalltalk implementation. (BTW, this post isn't
intended as a plug for Smalltalk -- though readers are welcome to take it that
way if they wish -- but as an experience report from someone who has used a
language with the features under discussion.)
The typical /implementation/ strategy is the old "unboxed representation" trick
(encoding smallish int values in what otherwise would be a pointer to an
object's physical representation -- /NOT/, please note, auto-boxing, which
would indeed be a performance problem). This does cause a small slowdown
relative to the use of "raw" primitive types, which -- in the case of
Smalltalk -- is exacerbated by the dynamic nature of its type system making it
harder (but not impossible) for the JIT to optimise arithmetic operations, but
the slowdown is fairly small (I don't have figures for modern implementations,
but I'd guess 10-20% for raw arithmetic) and would easily be accommodated for
/most/ purposes.
For integer values that need more than about 30 bits the system must switch to
a boxed representation. That indeed causes a noticeable slowdown (mainly
because of all the object allocations), but on the other hand there is no
problem with integer overflow (I still can't understand why Java -- supposedly
so safe -- allows /silent/ integer wraparound by default).
That's the implementation side. Now for the semantics. To be honest I don't
really understand what you think the problem would be. In Smalltalk integers
are objects, and that causes no problems that I can see. It's not obvious to
me what value "value semantics" would be supposed to add. Integers are either
BigIntegers (boxed) or SmallIntegers (unboxed), both are subclasses of Integer.
Integer and Float are subclasses of Number. Normal class-based polymorphism is
used extensively and in complete accordance with how all the other objects
work. No need for "widening conversions", overloaded arithmetic operators (0.1
+ 22) and all the other junk that Java has to add to its semantics in order to
hack around the problems caused by primitive types. It all works very well.
BTW, I'm not saying that it would be appropriate for a language like Java not
to have primitive types at all. The nature of the language (specifically its
type system) means that they could be included as an expert-friendly feature
available for use by people who knew what they were doing when (say) adding two
integers together, or comparing two floats. But what I /do/ think is that
machine primitives should never have been the /default/ representation of
numbers.
-- chris