📅  最后修改于: 2020-09-24 11:08:18             🧑  作者: Mango
Java中的包装器类提供了将原语转换为对象并将对象转换为原语的机制。
从J2SE5.0开始,自动装箱和拆箱功能将原语转换为对象,并将对象自动转换为原语。将原语自动转换为对象的过程称为自动装箱,反之亦然。
Java是一种面向对象的编程语言,因此我们需要多次处理对象,例如在Collections,Serialization,Synchronization等中。让我们看一下需要使用包装器类的不同场景。
java.lang包的八个类在Java中称为包装器类。八个包装器类的列表如下:
Primitive Type | Wrapper class |
---|---|
boolean | Boolean |
char | Character |
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Floa |
double | Double |
将原始数据类型自动转换为其对应的包装器类称为自动装箱,例如,字节到字节,字符到字符,整数到整数,长到长,浮点到浮点,布尔值到布尔,双精度到双精度和短太短。
从Java5开始,我们不需要使用包装器类的valueOf()方法将原语转换为对象。
包装器类示例:包装器的基元
"//Java program to convert primitive into objects
//Autoboxing example of int to Integer
public class WrapperExample1{
public static void main(String args[]){
//Converting int into Integer
int a=20;
Integer i=Integer.valueOf(a);//converting int into Integer explicitly
Integer j=a;//autoboxing, now compiler will write Integer.valueOf(a) internally
System.out.println(a+" "+i+" "+j);
}}
输出:
20 20 20
将包装程序类型自动转换为其对应的原始类型的操作称为拆箱。这是自动装箱的反向过程。从Java5开始,我们不需要使用包装器类的intValue()方法将包装器类型转换为基元。
包装器类示例:包装器到基元
"//Java program to convert object into primitives
//Unboxing example of Integer to int
public class WrapperExample2{
public static void main(String args[]){
//Converting Integer to int
Integer a=new Integer(3);
int i=a.intValue();//converting Integer to int explicitly
int j=a;//unboxing, now compiler will write a.intValue() internally
System.out.println(a+" "+i+" "+j);
}}
输出:
3 3 3
"//Java Program to convert all primitives into its corresponding
//wrapper objects and vice-versa
public class WrapperExample3{
public static void main(String args[]){
byte b=10;
short s=20;
int i=30;
long l=40;
float f=50.0F;
double d=60.0D;
char c='a';
boolean b2=true;
//Autoboxing: Converting primitives into objects
Byte byteobj=b;
Short shortobj=s;
Integer intobj=i;
Long longobj=l;
Float floatobj=f;
Double doubleobj=d;
Character charobj=c;
Boolean boolobj=b2;
//Printing objects
System.out.println("---Printing object values---");
System.out.println("Byte object: "+byteobj);
System.out.println("Short object: "+shortobj);
System.out.println("Integer object: "+intobj);
System.out.println("Long object: "+longobj);
System.out.println("Float object: "+floatobj);
System.out.println("Double object: "+doubleobj);
System.out.println("Character object: "+charobj);
System.out.println("Boolean object: "+boolobj);
//Unboxing: Converting Objects to Primitives
byte bytevalue=byteobj;
short shortvalue=shortobj;
int intvalue=intobj;
long longvalue=longobj;
float floatvalue=floatobj;
double doublevalue=doubleobj;
char charvalue=charobj;
boolean boolvalue=boolobj;
//Printing primitives
System.out.println("---Printing primitive values---");
System.out.println("byte value: "+bytevalue);
System.out.println("short value: "+shortvalue);
System.out.println("int value: "+intvalue);
System.out.println("long value: "+longvalue);
System.out.println("float value: "+floatvalue);
System.out.println("double value: "+doublevalue);
System.out.println("char value: "+charvalue);
System.out.println("boolean value: "+boolvalue);
}}
输出:
---Printing object values---
Byte object: 10
Short object: 20
Integer object: 30
Long object: 40
Float object: 50.0
Double object: 60.0
Character object: a
Boolean object: true
---Printing primitive values---
byte value: 10
short value: 20
int value: 30
long value: 40
float value: 50.0
double value: 60.0
char value: a
boolean value: true
Java包装器类包装原始数据类型,这就是为什么它被称为包装器类的原因。我们还可以创建一个包装原始数据类型的类。因此,我们可以使用Java创建自定义包装类。
"//Creating the custom wrapper class
class Javatpoint{
private int i;
Javatpoint(){}
Javatpoint(int i){
this.i=i;
}
public int getValue(){
return i;
}
public void setValue(int i){
this.i=i;
}
@Override
public String toString() {
return Integer.toString(i);
}
}
//Testing the custom wrapper class
public class TestJavatpoint{
public static void main(String[] args){
Javatpoint j=new Javatpoint(10);
System.out.println(j);
}}
输出:
10