简体   繁体   English

java中的包装器和自动装箱/拆箱有什么区别?

[英]What is difference between wrapper and Auto Boxing/Unboxing in java?

A Wrapper Class is used to convert primitive into object and object into primitive. Wrapper类用于将原始类型转换为对象,将对象转换为原始类型。 Similarly by using Autoboxing and Unboxing we can do the same then what is the difference in these two: 1-Concept wise 2-Code wise???同样,通过使用AutoboxingUnboxing Autoboxing ,我们可以做同样的事情,那么这两者有什么区别:1-概念明智 2-代码明智???

Auto-boxing and auto-unboxing is just the compiler silently helping you create and use primitive wrapper objects.自动装箱和自动拆箱只是编译器默默地帮助您创建和使用原始包装器对象。

For example, the int primitive type has wrapper class calledInteger .例如, int原始类型具有称为Integer包装类。 You wrap and unwrap as follows:您按如下方式包装和解包:

int myInt = 7;

// Wrap the primitive value
Integer myWrappedInt = Integer.valueOf(myInt);

// Unwrap the value
int myOtherInt = myWrappedInt.intValue();

With auto-boxing and auto-unboxing, you don't have to do all that boiler-plate stuff:使用自动装箱和自动拆箱,您不必做所有的样板工作:

int myInt = 7;

// Wrap the primitive value
Integer myWrappedInt = myInt; // Compiler auto-boxes

// Unwrap the value
int myOtherInt = myWrappedInt; // Compiler auto-unboxes

It's just a syntactic sugar, handled by the compiler.它只是一个语法糖,由编译器处理。 The generated byte code is the same.生成的字节码是一样的。

Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. Autoboxing是 Java 编译器在原始类型与其对应的对象包装类之间进行的自动转换。 For example, converting an int to an Integer, a double to a Double, and so on.例如,将 int 转换为 Integer,将 double 转换为 Double,等等。 If the conversion goes the other way, this is called unboxing .如果转换以另一种方式进行,则称为unboxing

Here is the simplest example of autoboxing:这是自动装箱的最简单示例:

 Character ch = 'a';

The wrapper classes provide a mechanism to "wrap" primitive values in an object so that the primitives can be included in activities only for objects, like being added to Collections. wrapper类提供了一种将原始值“包装”在对象中的机制,以便原始值可以仅包含在对象的活动中,例如添加到集合中。 There is a wrapper class for every primitive in Java. Java 中的每个原语都有一个包装类。 The wrapper class for int is Integer and the class for float is Float and so on. int 的包装类是 Integer,而 float 的类是 Float 等等。 Wrapper classes also provide many utility functions for primitives like Integer.parseInt().包装类还为 Integer.parseInt() 等原语提供了许多实用函数。

Three most common approaches for creating wrapper objects are:创建包装器对象的三种最常见方法是:

1. Using constructor as 1. 使用构造函数作为

 Integer i1 = new Integer(42);

All of the wrapper classes except Character provide two constructors: one that takes a primitive type and one that takes a String representation of the primitive type: new Integer(42) or new Integer("42").除 Character 之外的所有包装类都提供了两种构造函数:一种采用原始类型,另一种采用原始类型的 String 表示形式:new Integer(42) 或 new Integer("42")。 The Character class provides only one constructor, which takes a char as an argument: new Character('c'). Character 类只提供了一个构造函数,它接受一个字符作为参数:new Character('c')。

2. Using valueOf() as 2. 使用 valueOf() 作为

 Float f2 = Float.valueOf("3.14f");

3. Directly assigning a primitive to a wrapper reference, in which case java will automatically create an object for you, which is called as Autoboxing as 3.直接给一个包装器引用赋值一个原语,在这种情况下java会自动为你创建一个对象,称为Autoboxing as

 Long weight = 1200L;

There are many utility functions for the wrapper class, mainly for conversions like:包装类有许多实用函数,主要用于转换,例如:

  double d4 = Double.parseDouble("3.14");

  Double d5 = Double.valueOf("3.14");

Wrapper classes in java provides a mechanism to convert primitive into object and object into primitive. java中的包装类提供了一种将原始类型转换为对象,将对象转换为原始类型的机制。 Whereas automatic boxing and unboxing allows you to do that conversion automatically.而自动装箱和拆箱允许您自动进行转换。 Autoboxing and auto unboxing are legal in java since Java 5.自 Java 5 以来,自动装箱和自动拆箱在 Java 中是合法的。

public class Main {

    public static void main(String[] args) {
        int x=100;
        Integer iob;
        iob=x;//illegal upto JDK1.4
        iob= Integer.valueOf(x); //legal=> Boxing,Wrapping
        iob=x; //Legal since JDK1.5=> Auto Boxing

    }
}

Here x is a primitive variable, iob is a reference variable.这里 x 是原始变量, iob是引用变量。 So only an address can be assigned into this iob reference.因此,只能将地址分配到此 iob 引用中。 iob=Integer.valueOf(x) will convert primitive integer into integer object. iob=Integer.valueOf(x)将原始整数转换为整数对象。 This conversion can be implied as wrapping.这种转换可以隐含为换行。 iob=x will do the same thing. iob=x会做同样的事情。 It also saves a lot of coding.它还节省了大量的编码。

public class Main {

    public static void main(String[] args) {
        Integer iob= new Integer(100);
        int x;

        x=iob;//illegal upto JDK1.4
        x=iob.intValue(); //unboxing,unwrapping

        x=iob; //legal since JDK1.5=> auto unboxing 

    }
}

Here iob.intValue() will take the value of integer object and it will assign that value into x.在这里, iob.intValue()将获取整数对象的值,并将该值分配给 x。 x=iob does the same thing except you don't need to write conversion code. x=iob做同样的事情,只是你不需要编写转换代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM