Possible Duplicate:
stori开发者_如何学Gong primitive values in a java collection?
My Java textbook says elements of a collection, for example ArrayList, cannot be primitive types. Is there a reason for this? I mean did someone at Sun decide on this or is there some barrier against doing it? I understand my example half-answers my question since ArrayList requires an object and primitives are not objects. But then I think why can't they have primitive types as well?
is there some barrier against doing it?
You could write near-identical versions of ArrayList
that were tailor made to store one of the non-class types, e.g. IntegerArrayList
and so on. The barrier against this is that there would be an explosion of such classes, as you'd multiply the number of primitive types by the number of collection types. In order to keep the standard collection framework manageable, this was ruled out.
To solve this more neatly in the language, you'd need generics to allow primitive types to serve as type parameters, and improve the interaction between arrays and generics.
Storing unwrapped primitives would dramatically complicate the collections code. Whereas, with the wrappers (Integer
for int
, etc.), the code is fairly straight-forward. For several years now, Java has supported "auto-boxing", which means that if you give an int
where an Integer
is expected, the int
is wrapped up in an Integer
instance for you (and vice-versa).
There are objects called "wrappers" that represent all of the primitive types. For example, there is a class called Integer
that supports int
. You can use the primitive wrappers to hold values in a Collection.
The problem with primitive types (at least until Java 5) is that they didn't extend from the base Object
class. All of the collections need to specify a class for all the methods they are using - and they specify Object
, since Object
is the base of all the classes.
As of Java 5, you will find that Java will implicitly switch between a primitive and it's corresponding wrapper class when you need it. This means you can add an int
, or a double
, etc. to a Collection. The VM will automatically wrap the primitive in a wrapper class for you and place the wrapper in the Collection.
Currently the only way to store primtives directly into a collection, is to have a collection for each primitive type e.g. TIntArrayList.
You are likely to find that even though ArrayList is slower than using primitives, it is fast enough for 90+% of use cases.
Read this article on wikipedia. It might help: http://en.wikipedia.org/wiki/Object_type_(object-oriented_programming)#Autoboxing
In computer science, an object type (a.k.a. wrapping object) is a datatype which is used in object-oriented programming to wrap a non-object type to make it look like a dynamic object.
Some object-oriented programming languages make a distinction between reference and value types, often referred to as objects and non-objects on platforms where complex value types don't exist, for reasons such as runtime efficiency and syntax or semantic issues. For example, Java has primitive wrapper classes corresponding to each primitive type: Integer and int, Character and char, Float and float, etc. Languages like C++ have little or no notion of reference type; thus, the use of object type is of little interest.
Boxing is the process of placing a primitive type within an object so that the primitive can be used as a reference object. For example, lists may have certain methods which arrays might not, but the list might also require that all of its members be dynamic objects. In this case, the added functionality of the list might be unavailable to a simple array of numbers. For a more concrete example, in Java, a LinkedList can change its size, but an array must have a fixed size. One might desire to have a LinkedList of ints, but the LinkedList class only lists references to dynamic objects — it cannot list primitive types, which are value types.
To circumvent this, ints can be boxed into Integers, which are dynamic objects, and then added to a LinkedList of Integers. (Using generic parameterized types introduced in J2SE 5.0, this type is represented as LinkedList.) On the other hand, C# has no primitive wrapper classes, but allows boxing of any value type, returning a generic Object reference.
The boxed object is always a copy of the value object, and is usually immutable. Unboxing the object also returns a copy of the stored value. Note that repeated boxing and unboxing of objects can have a severe performance impact, since it dynamically allocates new objects and then makes them eligible for Garbage collection.
Performance issue is one of the problems since we need auto-boxing for this to be achieved. Also some of the structures may benefit from having null values also.
精彩评论