简体   繁体   English

java中的包装类是一个原始数据类型为成员的类吗?

[英]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中, “拳击转换”涵盖了这一点。

这意味着自动拳击到位。

You can always find the latest OpenJDK Integer class here : 您可以在此处找到最新的OpenJDK Integer类:

The relevant field is (from line 645): 相关领域(来自第645行):

/**
 * The value of the {@code Integer}.
 *
 * @serial
 */
private final int value;

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

相关问题 Java原始数据类型字节和类Byte - Java primitive data type byte and class Byte 方法参数中的原始类型和包装器类 - Primitive type and wrapper class within method argument 何时使用包装类和原始类型 - When to use wrapper class and primitive type 指向原始值的Java Wrapper类引用 - Java Wrapper class reference pointing to a primitive value 在实体 class 包装器 class 或 JPA/Hibernate 中的原始类型中选择属性数据类型的最佳实践是哪一个? - Which one is the best practice for choosing data type of a property in entity class Wrapper class or Primitive type in JPA/Hibernate? 基本数据类型的性能与其包装器类的关系 - Performance of Primitive Data types VS their Wrapper class JAXB编译器将xs:boolean绑定到Java布尔包装类,而不是布尔基元类型 - JAXB compiler is binding xs:boolean to Java Boolean wrapper class, instead of boolean primitive type Java ArrayList:添加原始类型或其包装器类:有什么区别? - Java ArrayList: Adding primitive type or its wrapper-class: What's the difference? 在JAVA中,原始数据类型如“ int”是CLASS还是OBJECT? - In JAVA, a primitive data type like “int” a CLASS or an OBJECT? 基本类型和包装器类之间的主要区别是什么? - What is the main difference between primitive type and wrapper class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM