[英]Wrapper class in java is it a class with primitive data type as member?
I want to know how Integer class works: Consider 我想知道Integer类是如何工作的:考虑一下
Integer number=2;
Does this mean, "Integer" class has a constructor like mentioned below and it stores the int value in it? 这是否意味着,“Integer”类有一个如下所述的构造函数,它将int值存储在其中? Please explain.
请解释。
class Integer
{
int a;
public Integer (int a)
{
this.a=a;
}
}
Pretty close. 八九不离十。 Check out the source code for Integer (apparently from Harmony so the Sun/Oracle JVM may be a bit different).
查看Integer的源代码 (显然来自Harmony,因此Sun / Oracle JVM可能会有所不同)。 Autoboxing conversions (when you assign a primitive to a wrapper class) use the equivalent of valueOf, which caches "common" integers and creates new ones for the rest.
自动装箱转换(当您将基元分配给包装类时)使用等价的valueOf,它会缓存“常用”整数并为其余整数创建新的整数。
javac
generates code to call Integer.valueOf(int)
which may or may not construct a new Integer
or just reuse an existing one. javac
生成调用Integer.valueOf(int)
代码,它可能构建或不构造新的Integer
,也可以只重用现有的Integer
。 In the JLS this is covered by "boxing conversions" . 在JLS中, “拳击转换”涵盖了这一点。
这意味着自动拳击到位。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.