What is the difference between Java and C + + objects created through new?

preface

In this article, we will not talk about basic data types such as int, float and char, but use general classes to illustrate them. Because in Java, a basic type variable can be defined and used directly through int Varname, but for other general types of objects, new must be used to create them.

Therefore, in order to analyze more generally and reflect the differences between the two languages in creating objects, we use the custom class student to explain. The following contents are for general classes.

Java

In Java, we can define variables in the following ways:

When the JVM performs memory management, it will first allocate a space for dog in the stack when new dog(); After that, we will open up the actual space of the object in the heap, and then point the dog to the space in the heap, so that we can the member variables of the method object.

Figure 3 memory allocation in Java object creation mode

c++

In C + +, variables can also be defined in the same way, but their meanings are different:

Both methods can create objects in C + +, but the processing in memory is completely different.

For the first method, the dog is stored in the stack, and the occupied size is the sum of the memory occupied by the member variables in the dog class. The member method is not included here, because the member method is stored in the public storage area so that all objects of this class can be accessed.

Figure 1 memory allocation of C + + object creation mode 1

The second method is different. This method uses pointers. When defining * P, it opens up a 4-byte space in the stack. When new dog(), it opens up a space in the heap, and then assigns the first address of the space to * P. in this way, any member method of the object in the heap can be found through * P.

Figure 2 memory allocation of C + + object creation mode 2

Difference summary

Both languages contain the new operator, but their meanings are very different, which is related to their variable management methods.

In Java, only when you use the new operator will you really apply for a space in memory, create a new object, and bind the object to the variable name you define. In other cases, either the existing object is bound to a variable name, or the defined variable name is an empty reference without binding any object.

In other words, defining a variable name only creates a new identifier, which has nothing to do with creating an object. Creating an object must be completed through new. Memory space will be applied only when creating an object.

But in C + +, when you define a variable s, even if you do not assign a value to it, it means that you not only create a new identifier, but also apply for the corresponding memory space in the stack.

Therefore, the variable name defined in C + + is not only an identifier, but also automatically associated with a memory space in the stack.

The new operator in C + + means to apply for memory in the heap. Because the memory in the stack is fixed and limited at run time, it needs to be implemented with new when dynamic memory allocation is required. This is similar to the malloc function in C, but the new operator also encapsulates other operations.

In summary, the variable name in Java is just an identifier used to refer to the actual object in memory. If you don't associate it with an object, it will be null. Although the variable name (non pointer type) in C + + is also an identifier, it is always associated with the actual memory space. When we see a variable (non pointer type), we know that it represents an actual memory space.

summary

The above is the whole content of this article. I hope the content of this article has a certain reference value for your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>