简体   繁体   English

Java中原始类型的数组如何工作?

[英]How array of primitive types work in Java?

In java, It is said that "every thing instantiated other than primitive type is an object." 在java中,据说“除了原始类型之外的所有实例化都是一个对象。”

I am trying to understand this point using below code (line 4 to be specific). 我试图使用下面的代码来理解这一点(第4行是具体的)。

public class dummy{
    public static void main(String[] args){
        int[] i = new int[2];
        i[0] = new Integer(3); //line 4
        i[1] = 3;
        System.out.println(int[].class);
        System.out.println(i[0]);
    }
}

After running 跑完之后

int[] i = new int[4];

Java is creating an object [0,0] of type class [I . Java正在创建类型为class [I [0,0]的对象[0,0] The two members within this object [0,0] are primitive data type(but not reference type). 此对象[0,0]中的两个成员是原始数据类型(但不是引用类型)。

My questions: 我的问题:

  1. Why does Java allow assignment of an object to primitive type member as per below line? 为什么Java允许将对象分配给基本类型成员,如下所示?

     i[0] = new Integer(3); // object of type 'class Integer' getting assigned to i[0] 

    How do I understand this? 我怎么理解这个? In counter, i[0] displays value 3 but not object address. 在计数器中, i[0]显示值3但不显示对象地址。

  2. What is I in class [I ? Iclass [I是什么时候class [I I mean, for class C{}; 我的意思是,对于class C{}; , C[].class gives class [LC where [ means "array of" and LC means "instance of 'class C'" C[].class给出class [LC其中[表示“数组”和LC表示“实例'类C'”

i[0] = new Integer(3);

This involves what's known as auto-unboxing . 这涉及所谓的自动拆箱 In the Dark Ages of Java (pre-1.5) we used to have to explicitly box and unbox primitive types. 在Java的黑暗时代(1.5之前版本),我们过去必须明确地选择基本类型和拆箱原型。 But thankfully the language designers had pity on our poor fingers and added some syntactical sugar. 但幸运的是语言设计师对我们可怜的手指表示同情,并添加了一些语法糖。 They now let us convert freely between primitives and their wrapper types. 它们现在让我们在原语和它们的包装类型之间自由转换。 Java secretly transforms the above code into this: Java秘密地将上面的代码转换成:

i[0] = new Integer(3).intValue();

That's auto-unboxing. 这是自动拆箱。 Similarly, it will box values for you without your having to ask. 同样,它会为您提供价值,而无需您提出要求。 If you write: 如果你写:

Integer i = 5;

Java will actually perform: Java实际上会执行:

Integer i = Integer.valueOf(5);

What is I in class [I ? 我在class [I是什么时候class [I

That's I for int . 那是Iint

  1. Java is expecting you to assign an int to an element of the array, but you pass Integer , so it is automatically unboxed as int . Java期望您将int分配给数组的元素,但是您传递Integer ,因此它会自动取消装箱int By the way, when you create an Integer[] array, you will also get the same result when doing System.out.println , because Integer.toString just creating the string of it's value, not "object address". 顺便说一句,当你创建一个Integer[]数组时,你在执行System.out.println时也会得到相同的结果,因为Integer.toString只是创建它的值的字符串,而不是“对象地址”。
  2. [ means one-dimensional array. [表示一维数组。 I means int . I意思是int

java/lang/Integer.intValue:()I is being called when you do i[0] = new Integer(3); //line 4 java/lang/Integer.intValue:()I当你执行i[0] = new Integer(3); //line 4时, java/lang/Integer.intValue:()I被调用i[0] = new Integer(3); //line 4 i[0] = new Integer(3); //line 4 ie, the compiler is implicitly getting the int value of the Integer and adding it in the array. i[0] = new Integer(3); //line 4 ie,编译器隐式获取Integer的int值并将其添加到数组中。

For your first question, Java autoboxes primitive types with their wrapper classes (for example, int gets wrapped into the Integer class) and also unboxes wrappers of primitive types. 对于您的第一个问题,Java autoboxes原始类型及其包装类(例如,int被包装到Integer类中),并且还取消了原始类型的包装器。

Here is the documentation: http://docs.oracle.com/javase/tutorial/java/data/autoboxing.html 这是文档: http//docs.oracle.com/javase/tutorial/java/data/autoboxing.html

编译器执行基元与其对应对象之间的转换,以方便程序员。

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

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