2012-05-30 25 views
1

可能重复:
How are Integer arrays stored internally, in the JVM?Java如何处理原始类型数组?

在C#,当创建一个新的数组,其是一个引用类型所以它把一个指针到堆栈和堆在对象本身。如果你创建这个数组与简单的原始类型,如intdouble,等等。它的作用是把价值观正是它们被放置在堆,而不是指向在哪里存储它的内容的另一堆地址的指针。

所以,可以有人请解释这是如何发生在Java中? Java在数组中始终使用Integer(引用类型),或将值类型视为C#呢?

int[] hello = new int[5]; 
hello[0] = 2; // C# put this value directly in same slot and doesn't 
//create a wrapping object. 

我知道一个叫做C#没有的Java环绕类型的东西。 C#有自动装箱但Int32可以说不是一个引用类型,但ValueType其中作为Integer是相对于int的对象。你可以使用Object o = 5;来填充一个值,或者如果struct有一个父类,你也可以使用它来包装堆(装箱)。

+2

http://stackoverflow.com/questions/76549/how-are-integer-arrays-stored-internally-in-the-jvm – theglauber

+2

没有问题存在。然而,尽管C#中的'List '很好,但Java不喜欢这样。 – harold

+0

http://stackoverflow.com/questions/5564423/arrays-in-java-and-how-they-are-stored-in-memory – theglauber

回答

3

Java与您描述的非常相似。

int[] hello = new int[5]; // reference hello is on stack, the object is on the heap. 
hello[0] = 2; // Java puts this value directly in same slot and doesn't 
       // create a wrapping object. 
+2

我真的很喜欢你的答案:)快速简洁... – Tarik

2

Java原始数组作为原语的数组存储在堆中,而不是整数等。我不相信它们如何存储的实际实现被指定,所以布尔[]很可能由内存中的int []

1

在Java中,阵列被认为是是否持有原始变量或对象类型,在Java数组具有一个和仅称为长度的一个实例变量的Object。

INT [] ARR =新INT [5];这里

ARR是一个对象引用数组变量,如果其被存储在STACK其使用Inside的方法(即,作为local variableBut如果其作为instance variable使用,则其存储在Heap所述对象内部。

+0

感谢答复库马尔,但它似乎并没有谈论同样的事情。我知道那些,但我想知道Java如何使用数组在堆中存储原始类型。那么,我在之前的答案中得到了答案。 – Tarik