<legend id='ZIQGs'><style id='ZIQGs'><dir id='ZIQGs'><q id='ZIQGs'></q></dir></style></legend>

      <i id='ZIQGs'><tr id='ZIQGs'><dt id='ZIQGs'><q id='ZIQGs'><span id='ZIQGs'><b id='ZIQGs'><form id='ZIQGs'><ins id='ZIQGs'></ins><ul id='ZIQGs'></ul><sub id='ZIQGs'></sub></form><legend id='ZIQGs'></legend><bdo id='ZIQGs'><pre id='ZIQGs'><center id='ZIQGs'></center></pre></bdo></b><th id='ZIQGs'></th></span></q></dt></tr></i><div id='ZIQGs'><tfoot id='ZIQGs'></tfoot><dl id='ZIQGs'><fieldset id='ZIQGs'></fieldset></dl></div>
      1. <small id='ZIQGs'></small><noframes id='ZIQGs'>

        <tfoot id='ZIQGs'></tfoot>
          <bdo id='ZIQGs'></bdo><ul id='ZIQGs'></ul>

        int 数组初始化

        int array initialization(int 数组初始化)

          <legend id='QgDIi'><style id='QgDIi'><dir id='QgDIi'><q id='QgDIi'></q></dir></style></legend>
          <i id='QgDIi'><tr id='QgDIi'><dt id='QgDIi'><q id='QgDIi'><span id='QgDIi'><b id='QgDIi'><form id='QgDIi'><ins id='QgDIi'></ins><ul id='QgDIi'></ul><sub id='QgDIi'></sub></form><legend id='QgDIi'></legend><bdo id='QgDIi'><pre id='QgDIi'><center id='QgDIi'></center></pre></bdo></b><th id='QgDIi'></th></span></q></dt></tr></i><div id='QgDIi'><tfoot id='QgDIi'></tfoot><dl id='QgDIi'><fieldset id='QgDIi'></fieldset></dl></div>
        1. <small id='QgDIi'></small><noframes id='QgDIi'>

        2. <tfoot id='QgDIi'></tfoot>

            <bdo id='QgDIi'></bdo><ul id='QgDIi'></ul>

                    <tbody id='QgDIi'></tbody>
                  本文介绍了int 数组初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在这里有一个与 Java 相关的简单问题.假设您有一个 int 数组作为实例变量:

                  I have here a simple question related to Java. Let's say you have an int array as instance variable:

                  int[] in = new int[5];
                  

                  所以,现在默认情况下它包含 5 个零.但是,如果您有与局部变量相同的数组怎么办.它是否被初始化为零?那不是家庭作业,我正在学习 Java 语言.最好的问候

                  So, now by default it contains 5 zeros. But what if you have the same array as local variable. Does it get initialized to zeros? That is not a homework, I am learning Java language. Best regards

                  推荐答案

                  首先要了解的是,本地变量存储在堆栈 未使用其默认值显式初始化.而 实例变量 存储在 Heap 中,并且默认情况下使用它们的 默认值 进行初始化.

                  First thing to understand is that, local varibles are stored on stack which are not initialized explicitly with their default values. While instance variables are stored on Heap, and they are by default initialized with their default value.

                  此外,对象也在 Heap 上创建,无论实例引用变量是持有其引用还是局部引用变量.

                  Also, objects are also created on Heap, regardless of whether an instance reference variable is holding its reference, or a local reference variable.

                  现在,当您将这样的数组引用声明为局部变量并使用数组对其进行初始化时,会发生什么:-

                  Now, what happens is, when you declare your array reference like this as local variable, and initialize it with an array: -

                  int[] in = new int[5];
                  

                  数组引用(in)存储在stack上,并且为数组分配了内存,该数组能够在上保存5个整数元素em>heap(记住,对象是在堆上创建的).然后,在 Heap 上分配 5 个连续的内存位置 (size = 5),用于存储 integer 值.并且数组对象上的每个 index 都按顺序保存了对这些内存位置的 reference.然后数组引用指向该数组.因此,由于在堆上分配了 5 个整数值的内存,因此将它们初始化为默认值.

                  The array reference (in) is stored on stack, and a memory is allocated for array capable of holding 5 integer elements on heap (Remember, objects are created on Heap). Then, 5 contiguous memory location (size = 5), for storing integer value are allocated on Heap. And each index on array object holds a reference to those memory location in sequence. Then the array reference points to that array. So, since memory for 5 integer values are allocated on Heap, they are initialized to their default value.

                  而且,当你声明你的数组引用时,不要用任何数组对象初始化它:-

                  And also, when you declare your array reference, and don't initialize it with any array object: -

                  int[] in;
                  

                  数组引用是在 Stack 上创建的(因为它是一个局部变量),但默认情况下它不会初始化为数组,也不会初始化为 null,与实例变量一样.

                  The array reference is created on Stack (as it is a local variable), but it does not gets initialized to an array by default, and neither to null, as is the case with instance variables.

                  所以,这就是使用第一种数组声明和初始化方式时的分配方式:-

                  So, this is how allocation looks like when you use the first way of array declaration and initialization: -

                  "Your array reference"
                       "on stack"    
                  
                         |    |          "Array object on Heap"
                         +----+                  
                         | in |---------->  ([0, 0, 0, 0, 0])
                         +----+
                         "Stack"                  "Heap"
                  

                  这篇关于int 数组初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How can I detect integer overflow on 32 bits int?(如何检测 32 位 int 上的整数溢出?)
                  Local variables before return statements, does it matter?(return 语句之前的局部变量,这有关系吗?)
                  How to convert Integer to int?(如何将整数转换为整数?)
                  How do I create an int array with randomly shuffled numbers in a given range(如何在给定范围内创建一个随机打乱数字的 int 数组)
                  Inconsistent behavior on java#39;s ==(java的行为不一致==)
                  Why is Java able to store 0xff000000 as an int?(为什么 Java 能够将 0xff000000 存储为 int?)
                    <tfoot id='EXmOY'></tfoot>

                        <small id='EXmOY'></small><noframes id='EXmOY'>

                          <tbody id='EXmOY'></tbody>

                        <legend id='EXmOY'><style id='EXmOY'><dir id='EXmOY'><q id='EXmOY'></q></dir></style></legend>
                        • <bdo id='EXmOY'></bdo><ul id='EXmOY'></ul>

                            <i id='EXmOY'><tr id='EXmOY'><dt id='EXmOY'><q id='EXmOY'><span id='EXmOY'><b id='EXmOY'><form id='EXmOY'><ins id='EXmOY'></ins><ul id='EXmOY'></ul><sub id='EXmOY'></sub></form><legend id='EXmOY'></legend><bdo id='EXmOY'><pre id='EXmOY'><center id='EXmOY'></center></pre></bdo></b><th id='EXmOY'></th></span></q></dt></tr></i><div id='EXmOY'><tfoot id='EXmOY'></tfoot><dl id='EXmOY'><fieldset id='EXmOY'></fieldset></dl></div>