简体   繁体   English

静态数组如何存储在Java内存中?

[英]How are static arrays stored in Java memory?

So in a language like C, memory is separated into 5 different parts: OS Kernel, text segment, static memory, dynamic memory, and the stack. 所以在像C这样的语言中,内存分为5个不同的部分:OS内核,文本段,静态内存,动态内存和堆栈。 Something like this: 像这样的东西:

内存布局

If we declared a static array in C, you had to specify it's size beforehand after that would be fixed forevermore. 如果我们在C中声明了一个静态数组,那么你必须事先指定它的大小,然后才能永久修复它。 The program would allocate enough memory for the array and stick it in the static data segment as expected. 该程序将为阵列分配足够的内存,并按预期将其粘贴在静态数据段中。

However I noticed that in Java, you could do something like this: 但是我注意到在Java中,你可以这样做:

public class Test {
        static int[] a = new int[1];

        public static void main( String[] args ) {
                a = new int[2];
        }
} 

and everything would work as you'd expect. 一切都会像你期望的那样奏效。 My question is, why does this work in Java? 我的问题是,为什么这在Java中有效?

EDIT: So the consensus is that an int[] in Java is acts more similarly to an int* in C. So as a follow up question, is there any way to allocate arrays in static memory in Java (if no, why not)? 编辑:所以共识是Java中的int[]更像C中的int* 。因此作为后续问题,有没有办法在Java中的静态内存中分配数组(如果没有,为什么不呢) ? Wouldn't this provide quicker access to such arrays? 这不会更快地访问这样的阵列吗? EDIT2: ^ this is in a new question now: Where are static class variables stored in memory? EDIT2:^现在这是一个新问题: 静态类变量存储在内存中的哪个位置?

The value of a is just a reference to an object. 的值a仅仅是对一个对象的引用。 The array creation expression ( new int[2] ) creates a new object of the right size, and assigns a reference to a . 数组创建表达式( new int[2]生成大小合适的一个对象,和基准分配给a

Note that static in Java is fairly separate to static in C. In Java it just means "related to the type rather than to any particular instance of the type". 需要注意的是static在Java中是相当独立的,以static的C.在Java它只是意味着“相关的类型,而不是类型的任何特定实例”。

In java any time you use the word new , memory for that object is allocated on the heap and a reference is returned. 在java中,只要你使用new这个词,就会在堆上分配该对象的内存并返回引用。 This is also true for arrays. 阵列也是如此。 The int[] a is just the reference to new int[1] . int[] a只是对new int[1]的引用。 When you do new int[2] , a new array is allocated and pointed to a. 当你执行new int[2] ,会分配一个新数组并指向a。 The old array will be garbage collected when needed. 旧数组将在需要时进行垃圾回收。

You are creating a new array, not modifying the old one. 您正在创建一个新数组,而不是修改旧数组。 The new array will get its own space and the old one will be garbage-collected (so long as nobody else holds a reference to it). 新数组将获得自己的空间,旧数组将被垃圾收集(只要没有其他人拥有对它的引用)。

I assume when you're referring to "static memory" you're referring to the heap. 我假设当你提到“静态内存”时,你指的是堆。 In Java, the heap serves a similar purpose to the "static data segment" you mentioned. 在Java中,堆的用途与您提到的“静态数据段”类似。 The heap is where most objects are allocated, including arrays. 堆是大多数对象分配的地方,包括数组。 The stack, on the other hand, is where objects that are used only during the life of a single method are placed. 另一方面,堆栈是放置仅在单个方法的生命期间使用的对象的位置。

In Java you've merely asked that a strongly typed reference to an array be stored statically for the class Test . 在Java中,您只是要求为类Test静态存储对数组的强类型引用。 You can change what a refers to at runtime, which includes changing the size . 你可以改变什么a是指在运行,其中包括改变大小 This would be the C equivalent of a static storage of an int* . 这将是C *相当于int*的静态存储。

In Java, a static variable exists as part of the class object. 在Java中, static变量作为类对象的一部分存在。 Think of it as an instance variable for the class itself. 可以将其视为类本身的实例变量。 In your example, a is a reference variable, which refers to some array (or no array at all, if it is null ), but the array itself is allocated as all arrays are in Java: off the heap. 在您的示例中, a是一个引用变量,它引用一些数组(或者根本没有数组,如果它是null ),但是数组本身是在所有数组都在Java中的时候分配的:离开堆。

Static has a different meaning in Java. 静态在Java中有不同的含义。 In Java when you declare a variable as static it is a class variable and not an instance variable. 在Java中,当您将变量声明为static时,它是一个类变量而不是实例变量。

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

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