Java中哪些需要包装类 它们将原始数据类型转换为对象。如果我们希望修改传递给方法的参数,则需要对象(因为基元类型是按值传递的)。
java.util包中的类只处理对象,因此在这种情况下包装类也有帮助。
Collection框架中的数据结构(如ArrayList和Vector)仅存储对象(引用类型)而不存储基本类型。
需要一个对象来支持多线程中的同步。
Java原始数据类型及其对应的包装类 基本数据类型 包装类
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean
Java中的自动装箱和拆箱
自动装箱:将原始类型自动转换为相应包装类的对象称为自动装箱。例如 - 将int转换为Integer,将long转换为Long,将double转换为Double等
示例: // Java program to demonstrate Autoboxing
import java.util.ArrayList;
class Autoboxing
{
public static void main(String[] args)
{
char ch = 'a';;
// Autoboxing- primitive to Character object conversion
Character a = ch;
ArrayList arrayList = new ArrayList();
// Autoboxing because ArrayList stores only objects
arrayList.add(25);
// printing the values from object
System.out.println(arrayList.get(0));
}
}
输出: 25
拆箱:这只是自动装箱的逆向过程。自动将包装类的对象转换为其对应的基元类型称为拆箱。例如 - 将Integer转换为int,Long转换为long,将Double转换为double等。 // Java program to demonstrate Unboxing
import java.util.ArrayList;
class Unboxing
{
public static void main(String[] args)
{
Character ch = 'a';
// unboxing - Character object to primitive conversion
char a = ch;
ArrayList arrayList = new ArrayList();
arrayList.add(24);
// unboxing because get method returns an Integer object
int num = arrayList.get(0);
// printing the values from primitive data types
System.out.println(num);
}
}
输出: 24
Java履行Implementation // Java program to demonstrate Wrapping and UnWrapping
// in Java Classes
class WrappingUnwrapping
{
public static void main(String args[])
{
// byte data type
byte a = 1;
// wrapping around Byte object
Byte byteobj = new Byte(a);
// int data type
int b = 10;
//wrapping around Integer object
Integer intobj = new Integer(b);
// float data type
float c = 18.6f;
// wrapping around Float object
Float floatobj = new Float(c);
// double data type
double d = 250.5;
// Wrapping around Double object
Double doubleobj = new Double(d);
// char data type
char e='a';
// wrapping around Character object
Character charobj=e;
// printing the values from objects
System.out.println("Values of Wrapper objects (printing as objects)");
System.out.println("Byte object byteobj: " + byteobj);
System.out.println("Integer object intobj: " + intobj);
System.out.println("Float object floatobj: " + floatobj);
System.out.println("Double object doubleobj: " + doubleobj);
System.out.println("Character object charobj: " + charobj);
// objects to data types (retrieving data types from objects)
// unwrapping objects to primitive data types
byte bv = byteobj;
int iv = intobj;
float fv = floatobj;
double dv = doubleobj;
char cv = charobj;
// printing the values from data types
System.out.println("Unwrapped values (printing as data types)");
System.out.println("byte value, bv: " + bv);
System.out.println("int value, iv: " + iv);
System.out.println("float value, fv: " + fv);
System.out.println("double value, dv: " + dv);
System.out.println("char value, cv: " + cv);
}
}
输出: Values of Wrapper objects (printing as objects)
Byte object byteobj: 1
Integer object intobj: 10
Float object floatobj: 18.6
Double object doubleobj: 250.5
Character object charobj: a
Unwrapped values (printing as data types)
byte value, bv: 1
int value, iv: 10
float value, fv: 18.6
double value, dv: 250.5
char value, cv: a