为什么 Java 中有包装类?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3579035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 02:05:41  来源:igfitidea点击:

Why are there wrapper classes in Java?

java

提问by zengr

I know what a wrapper class is, they wrap primitive types (e.g. int, double, etc) to objects of their respective class.

我知道包装类是什么,它们将原始类型(例如 int、double 等)包装到各自类的对象中。

But, why do we need Wrapper classes in the first place? Why not simply go with primitive types where we have them?

但是,为什么我们首先需要 Wrapper 类?为什么不简单地使用我们拥有它们的原始类型?

采纳答案by froadie

Several possible reasons:

几个可能的原因:

  • So that a null value is possible
  • To include in a Collection
  • To treat generically / polymorphically as an Object along with other Objects
  • 所以空值是可能的
  • 包含在集合中
  • 将通用/多态视为一个对象以及其他对象

回答by Adam

Am example of when wrappers are used would be in Collections, you can have an ArrayList<Integer>, but not an ArrayList<int>same with HashMaps etc. To get type safety we use generics and generics need objects not primitives.

使用包装器的例子是在集合中,你可以有一个ArrayList<Integer>,但ArrayList<int>HashMaps 等不同。为了获得类型安全,我们使用泛型,泛型需要对象而不是基元。

回答by ebt

One pragmatic reason off the top of my head is that Objects can be null, primitives cannot*. If I can't ensure that a function can return an int, using the wrapper is the only way to deal with getting the int I expect. Autoboxing takes care of the rest.

我头顶上的一个务实原因是对象可以为空,原语不能*。如果我不能确保函数可以返回 int,那么使用包装器是处理获取我期望的 int 的唯一方法。自动装箱负责其余的工作。

回答by neXus

Wrapper classes are used instead of primitive types when an Object is expected.

当需要 Object 时,使用包装类而不是原始类型。

In Collections for example, an ArrayList may contain instances of any subclass of Object, but because primitive types are not Objects, they could not be contained in the ArrayList.

例如,在 Collections 中,ArrayList 可能包含 Object 的任何子类的实例,但由于原始类型不是 Objects,它们不能包含在 ArrayList 中。

回答by samitgaur

Java is an object oriented programming language. I think you could also ask - why do we have primitives and why is everything not just an object?

Java 是一种面向对象的编程语言。我想你也可以问 -为什么我们有原语,为什么一切都不仅仅是一个对象?

Java designers kept the two separate to keep things simple. You use the wrappers when you need types that fit in the object oriented world - like polymorphism, collections etc. You use the primitives when you need efficiency.

Java 设计者将两者分开以保持简单。当您需要适合面向对象世界的类型时,您可以使用包装器 - 如多态性、集合等。当您需要效率时,您可以使用原语。

回答by Pazhaniyappan

Java is an object-oriented language and as said everything in java is an object. But what about the primitives? They are sort of left out in the world of objects, that is, they cannot participate in the object activities, such as being returned from a method as an object, and being added to a Collection of objects, etc. . As a solution to this problem, Java allows you to include the primitives in the family of objects by using what are called wrapper classes.

Java 是一种面向对象的语言,正如所说的,Java 中的一切都是一个对象。但是原始人呢?它们在对象的世界中有点被排除在外,也就是说,它们不能参与对象活动,例如作为对象从方法返回,以及被添加到对象的集合等。作为此问题的解决方案,Java 允许您使用所谓的包装类将原语包括在对象族中。

回答by SupriYo Saha

Wrapper Class:

包装类:

  1. Java uses primitive types, such as int, char, double to hold the basic data types supported by the language.

  2. Sometimes it is required to create an object representation of these primitive types.

  3. These are collection classes that deal only with such objects. One needs to wrap the primitive type in a class.

  4. To satisfy this need, java provides classes that correspond to each of the primitive types. Basically, these classes encapsulate, or wrap, the primitive types within a class.

  5. Thus, they are commonly referred to as type wrapper. Type wrapper are classes that encapsulate a primitive type within an object.

  6. The wrapper types are Byte, Short, Integer, Long, Character, Boolean, Double, Float.

  1. Java 使用原始类型,例如 int、char、double 来保存语言支持的基本数据类型。

  2. 有时需要创建这些原始类型的对象表示。

  3. 这些是仅处理此类对象的集合类。需要将原始类型包装在一个类中。

  4. 为了满足这种需要,java 提供了对应于每个原始类型的类。基本上,这些类封装或包装类中的原始类型。

  5. 因此,它们通常被称为类型包装器。类型包装器是将原始类型封装在对象中的类。

  6. 包装器类型有 Byte、Short、Integer、Long、Character、Boolean、Double、Float。

回答by Dileep Edakkoth

Wrapper classes are used to convert any primitive type into an object.The primitive data types are not objects, they do not belong to any class, they are defined in the language itself. While storing in data structures which support only objects, it is required to convert the primitive type to object first, so we go for wrapper class.

包装类用于将任何原始类型转换为对象。原始数据类型不是对象,它们不属于任何类,它们是在语言本身中定义的。存储在仅支持对象的数据结构中时,需要先将原始类型转换为对象,因此我们使用包装类。

回答by Kun

There are three reasons that you might use a Number object rather than a primitive:

您可能会使用 Number 对象而不是原始对象的三个原因:

  1. As an argument of a method that expects an object (often used when manipulating collections of numbers).
  2. To use constants defined by the class, such as MIN_VALUE and MAX_VALUE, that provide the upper and lower bounds of the data type.
  3. To use class methods for converting values to and from other primitive types, for converting to and from strings, and for converting between number systems (decimal, octal, hexadecimal, binary).
  1. 作为期望对象的方法的参数(通常在操作数字集合时使用)。
  2. 使用类定义的常量,例如 MIN_VALUE 和 MAX_VALUE,提供数据类型的上限和下限。
  3. 使用类方法将值与其他基本类型相互转换、与字符串相互转换以及在数字系统(十进制、八进制、十六进制、二进制)之间进行转换。

Source from:

来源:

The Numbers Classes

数字类