Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why there is no sizeof in java

For what design reason there is no sizeof operator in java? knowing that it is very useful in c++ and c# and how you can get the size of a certain type if needed?

like image 711
Sleiman Jneidi Avatar asked Dec 01 '11 18:12

Sleiman Jneidi


People also ask

Does Java have sizeof?

Similarly, there is is no sizeof() operator in Java. All primitive values have a predefined size, e.g. int is 4 bytes, char is 2 byte, short is 2 byte, long and float is 8 byte, and so on.

What is sizeof int in Java?

Since int in an integer type variable. So, the sizeof(int) simply implies the value of size of an integer. Whether it is a 32-bit Machine or 64-bit machine, sizeof(int) will always return a value 4 as the size of an integer.

What is the size of an object in Java?

This is 16 bits as it is only storing the address of the object as there is no user defined constructor. Hence, any object has two components for memory: 16 bits for memory (depends on system) memory for the data initialized.

What is sizeof () operator?

The sizeof operator yields the size in bytes of the operand, which can be an expression or the parenthesized name of a type.

Why sizeof () is not needed in Java?

Fallacy: Sizeof () is not needed because Java basic types' sizes are fixed Yes, a Java int is 32 bits in all JVMs and on all platforms, but this is only a language specification requirement for the...

Is there a sizeof () operator in Java?

Similarly, there is is no sizeof () operator in Java. All primitive values have a predefined size, e.g. int is 4 bytes, char is 2 byte, short is 2 byte, long and float is 8 byte, and so on. But, if you are missing the operator, then why not let's make it a coding task?

How to get the full size of an object in Java?

The full object size can be obtained as a closure over the entire object graph rooted at the starting object Note: Implementing any Java interface merely marks the class in question and does not add any data to its definition.

Why is the size of primitive types not specified in JVM?

Because the size of primitive types is explicitly mandated by the Java language. There is no variance between JVM implementations. Moreover, since allocation is done by the new operator depending on its argument there is no need to specify the amount of memory needed.


2 Answers

Because the size of primitive types is explicitly mandated by the Java language. There is no variance between JVM implementations.

Moreover, since allocation is done by the new operator depending on its argument there is no need to specify the amount of memory needed.

It would sure be convenient sometimes to know how much memory an object will take so you could estimate things like max heap size requirements but I suppose the Java Language/Platform designers did not think it was a critical aspect.

like image 112
maerics Avatar answered Oct 17 '22 20:10

maerics


In c is useful only because you have to manually allocate and free memory. However, since in java there is automatic garbage collection, this is not necessary.

like image 40
rabusmar Avatar answered Oct 17 '22 22:10

rabusmar