One reason Java can't obviously do away with non-object primitives (int
, etc.) is that it does not support native data members. Imagine:
class int extends object
{
// need some data member here. but what type?
public native int();
public native int plus(int x);
// several more non-mutating methods
};
On second thoughts, we know Java maintains internal data per object (locks, etc.). Maybe we could define class int
with no data members, but with native methods that manipulate this internal data.
Remaining issues: Constants -- but these can be dealt with similarly to strings. Operators are just syntactical sugar and +
and would be mapped do the plus
method at compile time, although we need to be careful that int.plus(float)
returns float
as does float.plus(int)
, and so on.
Ultimately I think the justification for primitives is efficiency: the static analysis needed to determine that an int
object can be treated purely as JVM integer value may have been considered too big a problem when the language was designed.