Java 为什么我们需要包装类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20697868/
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
Why we need wrapper class
提问by Sivakumar M
I understand what is a wrapper class, they primitive types (eg: int, double, etc) to objects of their respective class (eg: Integer, Double, etc).
我了解什么是包装类,它们是各自类的对象(例如:Integer、Double 等)的原始类型(例如:int、double 等)。
But, why we need Wrapper classes, why we chose objects of their respective class.
但是,为什么我们需要 Wrapper 类,为什么我们选择它们各自类的对象。
回答by Ashish
Java is an object-oriented language and can view everything as an object. A simple file can be treated as an object , an address of a system can be seen as an object , an image can be treated as an object (with java.awt.Image) and a simple data type can be converted into an object (with wrapper classes). This tutorial discusses wrapper classes. Wrapper classes are used to convert any data type into an object.
Java 是一种面向对象的语言,可以将一切视为对象。一个简单的文件可以看作一个对象,一个系统的地址可以看作一个对象,一个图像可以看作一个对象(用java.awt.Image),一个简单的数据类型可以转换成一个对象(与包装类)。本教程讨论包装类。包装类用于将任何数据类型转换为对象。
The primitive data types are not objects; they do not belong to any class; they are defined in the language itself. Sometimes, it is required to convert data types into objects in Java language. For example, upto JDK1.4, the data structures accept only objects to store. A data type is to be converted into an object and then added to a Stack or Vector etc. For this conversion, the designers introduced wrapper classes.
原始数据类型不是对象;他们不属于任何阶级;它们是在语言本身中定义的。有时,需要将数据类型转换为 Java 语言中的对象。例如JDK1.4之前的数据结构只接受对象存储。数据类型将被转换为对象,然后添加到堆栈或向量等。对于这种转换,设计者引入了包装类。
What are Wrapper classes?
什么是包装类?
As the name says, a wrapper class wraps (encloses) around a data type and gives it an object appearance. Wherever, the data type is required as an object, this object can be used. Wrapper classes include methods to unwrap the object and give back the data type. It can be compared with a chocolate. The manufacturer wraps the chocolate with some foil or paper to prevent from pollution. The user takes the chocolate, removes and throws the wrapper and eats it.
顾名思义,包装类将数据类型包裹(包围)并赋予其对象外观。无论何时需要将数据类型作为对象,都可以使用该对象。包装类包括解开对象并返回数据类型的方法。它可以与巧克力相提并论。制造商用一些箔纸或纸包裹巧克力以防止污染。用户拿起巧克力,取出并扔掉包装纸,然后吃掉它。
Observe the following conversion.
观察以下转换。
int k = 100;
Integer it1 = new Integer(k);
The int data type k is converted into an object, it1 using Integer class. The it1 object can be used in Java programming wherever k is required an object.
int 数据类型 k 被转换为一个对象,it1 使用 Integer 类。在 Java 编程中,只要需要 k 对象,就可以使用 it1 对象。
The following code can be used to unwrap (getting back int from Integer object) the object it1.
以下代码可用于解包(从 Integer 对象取回 int)对象 it1。
int m = it1.intValue();
System.out.println(m*m); // prints 10000
intValue() is a method of Integer class that returns an int data type.
intValue() 是 Integer 类的一个方法,它返回一个 int 数据类型。
Importance of Wrapper classes
包装类的重要性
There are mainly two uses with wrapper classes.
包装类主要有两种用途。
1)To convert simple data types into objects, that is, to give object form to a data type; here constructors are used.
1)将简单的数据类型转化为对象,即将对象形式赋予一种数据类型;这里使用构造函数。
2)To convert strings into data types (known as parsing operations), here methods of type parseXXX() are used.
2)要将字符串转换为数据类型(称为解析操作),这里使用 parseXXX() 类型的方法。
Features of the Java wrapper Classes.
Java 包装器类的特性。
1)Wrapper classes convert numeric strings into numeric values.
1)包装类将数字字符串转换为数值。
2)The way to store primitive data in an object.
2)在对象中存储原始数据的方式。
3)The valueOf() method is available in all wrapper classes except Character
3)valueOf() 方法在除 Character 之外的所有包装类中都可用
4)All wrapper classes have typeValue() method. This method returns the value of the object as its primitive type.
4)所有包装类都有 typeValue() 方法。此方法返回对象的值作为其原始类型。