Wrapper
类用于将基元转换为对象以及将对象转换为基元。类似地,通过使用 Autoboxing
和 Unboxing
我们可以做同样的事情,那么这两者有什么区别:
1-概念明智
2-代码明智???
最佳答案
自动装箱和自动拆箱只是编译器默默地帮助您创建和使用原始包装对象。
例如,int
基元类型具有名为 Integer
的包装类。 。您可以按如下方式包装和展开:
int myInt = 7;
// Wrap the primitive value
Integer myWrappedInt = Integer.valueOf(myInt);
// Unwrap the value
int myOtherInt = myWrappedInt.intValue();
通过自动装箱和自动拆箱,您不必执行所有样板操作:
int myInt = 7;
// Wrap the primitive value
Integer myWrappedInt = myInt; // Compiler auto-boxes
// Unwrap the value
int myOtherInt = myWrappedInt; // Compiler auto-unboxes
它只是一个语法糖,由编译器处理。生成的字节码是相同的。
关于java - java中的包装器和自动装箱/拆箱有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34173337/