13

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

Boann
  • 48,794
  • 16
  • 117
  • 146
Sleiman Jneidi
  • 22,907
  • 14
  • 56
  • 77
  • possible duplicate of [In Java, what is the best way to determine the size of an object?](http://stackoverflow.com/questions/52353/in-java-what-is-the-best-way-to-determine-the-size-of-an-object) – Andrew Marshall Dec 01 '11 at 18:24
  • http://stackoverflow.com/questions/4115239/sizeof-java-object – wkl Dec 01 '11 at 18:25
  • well collections have .size() method and arrays and string have .length(). do you need bytes specifically? – aishwarya Dec 01 '11 at 18:25
  • 10
    Can you explain why you think you need it? If so we might be able to help you solve your actual problem. – Mark Peters Dec 01 '11 at 18:25
  • 1
    What are you planning to do with the information? – Dave Newton Dec 01 '11 at 18:25
  • 5
    Could you specify a situation where it would be "very useful" in Java? I have never needed it. – toto2 Dec 01 '11 at 18:26
  • if you are using some un-managed resources such as a c++ library – Sleiman Jneidi Dec 01 '11 at 18:37
  • In JNA, `size()` is sufficient for a given `Structure`. Even object wrapper classes for primitive types (Integer, Byte, etc.) provide `SIZE` constant. – ee. Dec 02 '11 at 00:55

6 Answers6

22

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.

maerics
  • 151,642
  • 46
  • 269
  • 291
7

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.

rabusmar
  • 4,084
  • 1
  • 25
  • 29
  • I know sometimes it might be of use, but, as @maerics pointed out, probably the designers didn't think it was necessary to add it to the language specification. There are still ways to know an object's size tho, check one of the question comments for that. – rabusmar Dec 01 '11 at 18:33
1

In java, you don't work directly with memory, so sizeof is usually not needed, if you still want to determine the size of an object, check out this question.

Community
  • 1
  • 1
MByD
  • 135,866
  • 28
  • 264
  • 277
0

Size of operator present in c/c++ and c/c++ is machine dependent langauge so different data types might have different size on different machine so programmes need to know how big those data types while performing operation that are sensitive to size. Eg:one machine might have 32 bit integer while another machine might have 16 bit integer.

But Java is machine independent langauge and all the data types are the same size on all machine so no need to find size of data types it is pre defined in Java.

Ysp
  • 173
  • 1
  • 1
  • 11
0

Memory management is done by the VM in Java, perhaps this might help you: http://www.javamex.com/java_equivalents/memory_management.shtml

Samizdis
  • 1,591
  • 1
  • 17
  • 33
0

C needed sizeof because the size of ints and longs varied depending on the OS and compiler. Or at least it used to. :-) In Java all sizes, bit configurations (e.g. IEEE 754) are better defined.

EDIT - I see that @Maerics provided a link to the Java specs in his answer.

user949300
  • 15,364
  • 7
  • 35
  • 66