问题
Possible Duplicate:
When we have wrappers classes, why primitives are supported?
If there are Wrapper classes which make Java pure object-oriented language, then why are there Primitive datatypes which can be used in Java???
回答1:
For efficiency. Variables of primitive types contain the value directly; variables of non-primitive types are references, referring to an object stored somewhere else in memory.
Each time you need to use the value of a wrapper type, the JVM needs to lookup the object in memory to get at the value. This isn't needed for primitive types, because the variable contains the value itself, instead of a reference to an object that contains the value.
However, that doesn't explain why primitive types need to be explicitly visible in the Java programming language. The designers of the Java language and the JVM could have chosen to hide primitive types from the language itself, so that you could treat everything as an object; the compiler could then translate it under the covers to more efficient primitive types.
Some newer programming languages that run on the JVM (Groovy, Scala and others) let you do exactly that: in the language itself everything looks like an object, which you can for example call methods on, but below the covers the compiler translates them to primitives.
I guess that in the time the Java language was developed (in the first half of the 1990's) people didn't think of that, and now it's too late for a radical change in the language to allow this.
回答2:
The main reason primitive data type are there because, creating object, allocating heap is too costly and there is a performance penalty for it. As you may know primitive data types like int, float etc are most used, so making them as Objects would have been huge performance hit. So Java designers thought it would be better to make it as non-objects. And yes, wrappers exists in-case you are ready to compromise bit on performance but you require more facility of OOP. So in that case you can use wrappers. Hope this information helps you.
回答3:
For performance reasons. In some other languages like SmallTalk types like int or char are also objects, and the methods can be invoked on them. This is more theoretically correct but current implementations run slower. Primitive types are compromise between purity and performance.
Where more necessary (null value possible or to use with collection framework), Java provides wrapper classes like java.lang.Integer and similar.
来源:https://stackoverflow.com/questions/14477743/why-are-there-primitive-datatype-in-java