Java集合。为什么没有原始类型?(复制)

时间:2022-03-24 16:27:27

Possible Duplicate:
storing primitive values in a java collection?

可能的重复:在java集合中存储原始值?

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?

我的Java教科书说集合的元素,例如ArrayList,不能是原始类型。这有什么原因吗?我的意思是,有人在Sun公司做过决定吗?我理解我的例子一半回答我的问题,因为ArrayList需要一个对象和原语不是对象。但是我想为什么他们也不能有原始类型呢?

7 个解决方案

#1


7  

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.

您可以编写几乎相同的ArrayList版本,它们是为存储非类类型之一而定制的,例如IntegerArrayList等。这样做的障碍是,这类类类会大量出现,因为要将原始类型的数量乘以集合类型的数量。为了使标准集合框架易于管理,这被排除在外。

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.

要想在语言中更巧妙地解决这个问题,您需要泛型来允许基本类型作为类型参数,并改进数组和泛型之间的交互。

#2


5  

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).

存储未包装的原语将极大地使集合代码复杂化。然而,对于包装器(int等的整数),代码是相当直接的。几年来,Java一直支持“自动装箱”,这意味着如果您在期望整数的地方提供一个int,那么这个int将被包装在一个整数实例中(反之亦然)。

#3


4  

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.

有一些对象称为“包装器”,它们表示所有的原始类型。例如,有一个名为Integer的类支持int,您可以使用原始包装器来保存集合中的值。

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.

原始类型(至少在Java 5之前)的问题是它们没有从基对象类扩展。所有集合都需要为它们正在使用的所有方法指定一个类——并且它们指定对象,因为对象是所有类的基础。

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.

从Java 5开始,您将发现当您需要时,Java将隐式地在原语和相应的包装类之间切换。这意味着您可以向集合添加int或double等等。VM将自动将原语包装到包装器类中,并将包装器放置在集合中。

#4


1  

Read this article on wikipedia. It might help: http://en.wikipedia.org/wiki/Object_type_(object-oriented_programming)#Autoboxing

请阅读*上的这篇文章。它可能帮助:http://en.wikipedia.org/wiki/Object_type_(object-oriented_programming)#自动装箱

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.

一些面向对象的编程语言区分了引用和值类型(通常在不存在复杂值类型的平台上称为对象和非对象),原因包括运行时效率、语法或语义问题。例如,Java有与每个基元类型对应的基元包装类:Integer and int、Character and char、Float and Float等。因此,对对象类型的使用兴趣不大。

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.

装箱是在对象中放置基元类型的过程,以便可以将基元用作引用对象。例如,列表可能有某些数组可能没有的方法,但是列表也可能要求它的所有成员都是动态对象。在这种情况下,列表添加的功能可能对一个简单的数字数组不可用。对于更具体的示例,在Java中,LinkedList可以更改它的大小,但是数组必须具有固定的大小。有人可能希望拥有一个ints的LinkedList,但LinkedList类只列出对动态对象的引用——它不能列出原语类型,它们是值类型。

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.

为了避免这种情况,ints可以被封装成动态对象的整数,然后添加到整数链表中。(使用J2SE 5.0中引入的泛型参数化类型,这种类型表示为LinkedList。)另一方面,c#没有原始包装类,但是允许任何值类型的装箱,返回一个泛型对象引用。

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.

被框对象始终是值对象的副本,并且通常是不可变的。解压对象还返回存储值的副本。注意,对象的重复装箱和反装箱可能会产生严重的性能影响,因为它会动态地分配新对象,然后使它们适合进行垃圾收集。

#5


1  

Currently the only way to store primtives directly into a collection, is to have a collection for each primitive type e.g. TIntArrayList.

目前,将原始类型直接存储到集合中的惟一方法是为每个原始类型(例如TIntArrayList)收集一个集合。

You are likely to find that even though ArrayList is slower than using primitives, it is fast enough for 90+% of use cases.

您可能会发现,即使ArrayList比使用原语慢,但它对于90%以上的用例来说已经足够快了。

#6


0  

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.

性能问题是一个问题,因为我们需要自动装箱来实现它。同样,一些结构也可以从零值中获益。

#7


-1  

Why can Java Collections not directly store Primitives types?

为什么Java集合不能直接存储基元类型?

#1


7  

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.

您可以编写几乎相同的ArrayList版本,它们是为存储非类类型之一而定制的,例如IntegerArrayList等。这样做的障碍是,这类类类会大量出现,因为要将原始类型的数量乘以集合类型的数量。为了使标准集合框架易于管理,这被排除在外。

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.

要想在语言中更巧妙地解决这个问题,您需要泛型来允许基本类型作为类型参数,并改进数组和泛型之间的交互。

#2


5  

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).

存储未包装的原语将极大地使集合代码复杂化。然而,对于包装器(int等的整数),代码是相当直接的。几年来,Java一直支持“自动装箱”,这意味着如果您在期望整数的地方提供一个int,那么这个int将被包装在一个整数实例中(反之亦然)。

#3


4  

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.

有一些对象称为“包装器”,它们表示所有的原始类型。例如,有一个名为Integer的类支持int,您可以使用原始包装器来保存集合中的值。

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.

原始类型(至少在Java 5之前)的问题是它们没有从基对象类扩展。所有集合都需要为它们正在使用的所有方法指定一个类——并且它们指定对象,因为对象是所有类的基础。

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.

从Java 5开始,您将发现当您需要时,Java将隐式地在原语和相应的包装类之间切换。这意味着您可以向集合添加int或double等等。VM将自动将原语包装到包装器类中,并将包装器放置在集合中。

#4


1  

Read this article on wikipedia. It might help: http://en.wikipedia.org/wiki/Object_type_(object-oriented_programming)#Autoboxing

请阅读*上的这篇文章。它可能帮助:http://en.wikipedia.org/wiki/Object_type_(object-oriented_programming)#自动装箱

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.

一些面向对象的编程语言区分了引用和值类型(通常在不存在复杂值类型的平台上称为对象和非对象),原因包括运行时效率、语法或语义问题。例如,Java有与每个基元类型对应的基元包装类:Integer and int、Character and char、Float and Float等。因此,对对象类型的使用兴趣不大。

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.

装箱是在对象中放置基元类型的过程,以便可以将基元用作引用对象。例如,列表可能有某些数组可能没有的方法,但是列表也可能要求它的所有成员都是动态对象。在这种情况下,列表添加的功能可能对一个简单的数字数组不可用。对于更具体的示例,在Java中,LinkedList可以更改它的大小,但是数组必须具有固定的大小。有人可能希望拥有一个ints的LinkedList,但LinkedList类只列出对动态对象的引用——它不能列出原语类型,它们是值类型。

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.

为了避免这种情况,ints可以被封装成动态对象的整数,然后添加到整数链表中。(使用J2SE 5.0中引入的泛型参数化类型,这种类型表示为LinkedList。)另一方面,c#没有原始包装类,但是允许任何值类型的装箱,返回一个泛型对象引用。

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.

被框对象始终是值对象的副本,并且通常是不可变的。解压对象还返回存储值的副本。注意,对象的重复装箱和反装箱可能会产生严重的性能影响,因为它会动态地分配新对象,然后使它们适合进行垃圾收集。

#5


1  

Currently the only way to store primtives directly into a collection, is to have a collection for each primitive type e.g. TIntArrayList.

目前,将原始类型直接存储到集合中的惟一方法是为每个原始类型(例如TIntArrayList)收集一个集合。

You are likely to find that even though ArrayList is slower than using primitives, it is fast enough for 90+% of use cases.

您可能会发现,即使ArrayList比使用原语慢,但它对于90%以上的用例来说已经足够快了。

#6


0  

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.

性能问题是一个问题,因为我们需要自动装箱来实现它。同样,一些结构也可以从零值中获益。

#7


-1  

Why can Java Collections not directly store Primitives types?

为什么Java集合不能直接存储基元类型?