Cause
One character in Java accounts for two bytes, which differs slightly from C + +. The sizeof operator makes it easy to know the size of a variable type or an object in C + +, what about in Java?
Problem arises
Why doesn't Java provide the sizeof operator?
To answer this question, we can see from another point of view that is why the sizeof operator is provided in C/s + +. This makes it tempting to think about the differences in memory management between C + + and Java.
In C, the memory allocation and release tasks are given to the programmer, and when we try to allocate a heap of memory for an object with malloc, an inescapable question is, what is the size of the object that will be created?
This is not difficult to explain C provides the purpose of sizeof, sizeof can easily get the size of the object.
In C + +, we can use new to dynamically allocate memory. At the same time, the dependence on sizeof is greatly reduced. However, C + + still retains sizeof, primarily to maintain compatibility with C.
Back to the beginning of the question, why didn't Java provide sizeof?
Yes, as you can imagine, the JVM has provided us with convenience.
Java gives the management of memory directly to the JVM. If C + + has a dependency on sizeof, then Java is doing it completely. At the same time, Java is a completely new design of the fully object-oriented language, there is no problem of C + + backwards compatibility.
The question has an answer, but there seems to be a problem: is there some situation (often we need to debug the code), we really need to know the size of an object in memory, then what?
Caused by "Two bytes of one character in Java"