简体   繁体   English

JVM在原始类型的声明中分配内存,但不为非原始类型分配内存?

[英]JVM allocates memory at declaration for primitive types, but not for non-primitive types?

I am from a C/C++ background and very new to java, I am having difficulty in understanding variable declaration and memory allocation in java. 我来自C / C ++背景,对java很新,我很难理解java中的变量声明和内存分配。

when we write, 我们写的时候

myclass myobject;

we declare that myobject is a variable of type myclass. 我们声明myobject是myclass类型的变量。 We are not allocating memory to it. 我们没有为它分配内存。

int a;

It declares the variable a and also allocates memory equal to size of int in stack. 它声明变量a并且还在堆栈中分配等于int大小的内存。

Is it the case? 是这样的吗? Does the compiler allocate memory for the primitive data types but not for the non-primitive data types? 编译器是为原始数据类型分配内存,而不是为非原始数据类型分配内存?

I've raised a similar doubt here . 我在这里提出了类似的疑问。

Compiler does't allocate memory. 编译器不分配内存。 Its JVM who allocate memory 它的JVM分配内存

For primitive data type memory is allocated at time of declaration of those variable and memory is taken in that function local stack. 对于原始数据类型,在声明那些变量时分配存储器,并在该函数本地堆栈中获取存储器。
int x;

memory allocated in stack 4 byte 堆栈4字节分配的内存

When we use new operator then memory is allocated to heap which is the size of class's data member. 当我们使用new运算符时,内存被分配给堆,这是类的数据成员的大小。
MyClass object;

This is reference variable also takes size of 4 byte 这个引用变量也需要4个字节的大小

object = new MyClass();

new operator allocate memory in heap and size is sum of all individual data member's size of that class. new运算符在堆中分配内存,大小是该类的所有单个数据成员大小的总和。

Is it the case? 是这样的吗? Does the compiler allocate memory for the primitive data types but not for the non-primitive data types? 编译器是为原始数据类型分配内存,而不是为非原始数据类型分配内存?

First point to note is Compiler won't allocate any memory. 首先要注意的是Compiler不会分配任何内存。 JVM do that. JVM这样做。

Coming to actual question, Yes. 来到实际问题,是的。 For primitives weather you initialize it or not later, once you declare, the memory allocates. 对于原语天气你初始化它或者稍后,一旦你声明,内存分配。

Here is the amount of data for primitives 这是基元的数据量

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

For objects, at the time of initialization, the memory allocates. 对于对象,在初始化时,内存分配。 The size of memory really depends on the Class data. 内存的大小实际上取决于Class数据。

In java the declaration of the variable doesn't mean allocate memory space but means this variable can hold a reference of an object with the same type of its class(or subclass) later. 在java中,变量的声明并不意味着分配内存空间,而是意味着此变量可以在以后保存具有相同类型的类(或子类)的对象的引用。

MyClass myObject; // myObject has null value (declaration only) // myObject具有空值(仅限声明)

The memory space is allocating when you write : 在您编写时分配内存空间:

MyClass myObject = new MyClass(); // when we use the new operator (the allocating occurs ) //当我们使用new运算符时(分配发生)

Now myObject has the reference which points to the resulted object from new MyClass() , and the size of this object depends on the data fields in its class declaration. 现在myObject具有指向新MyClass()的结果对象的引用,并且此对象的大小取决于其类声明中的数据字段。

Conclusion 结论

The statement 该声明

int a; // allocate fixed size because int size is known and can't change it //分配固定大小,因为int size已知且无法更改

MyClass myObject; // has null value //具有空值

myObject = new MyClass(); // now has a reference of this object //现在有这个对象的引用

I wish this help you :) . 我希望这能帮到你:)。

In Java, when we declare a variable of a class type, only a reference is created (memory is not allocated for the object). 在Java中,当我们声明类类型的变量时,只创建一个引用(不为该对象分配内存)。 To allocate memory to an object, we must use new() and all objects are dynamically allocated on Heap. 要为对象分配内存,我们必须使用new()并在Heap上动态分配所有对象。

in C++ where objects can be allocated memory either on Stack or on Heap. 在C ++中,可以在Stack或Heap上为对象分配内存。 when we allocate the object using new(), the object is allocated on Heap. 当我们使用new()分配对象时,对象在Heap上分配。

Understanding Memory Management 了解内存管理

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

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