Why is there no sizeof in Java?
Asked Answered
V

6

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?

Violoncellist answered 1/12, 2011 at 18:21 Comment(8)
possible duplicate of In Java, what is the best way to determine the size of an object?Mcnally
https://mcmap.net/q/904025/-sizeof-java-object-duplicateAvouch
well collections have .size() method and arrays and string have .length(). do you need bytes specifically?Pintail
Can you explain why you think you need it? If so we might be able to help you solve your actual problem.Singleness
What are you planning to do with the information?Humerus
Could you specify a situation where it would be "very useful" in Java? I have never needed it.Valve
if you are using some un-managed resources such as a c++ libraryVioloncellist
In JNA, size() is sufficient for a given Structure. Even object wrapper classes for primitive types (Integer, Byte, etc.) provide SIZE constant.Floating
C
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.

Chuckhole answered 1/12, 2011 at 18:24 Comment(4)
guys, the same for c# .I think that there is design reasonVioloncellist
It's the consequence of a design.Nanettenani
"People criticized its absence in Java" is the design reason for many things in C#.Moue
C# is much more explicitly targeted at unmanaged/COM Interop than Java though.Mannish
S
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.

Set answered 1/12, 2011 at 18:24 Comment(1)
I know sometimes it might be of use, but, as @Chuckhole 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.Set
H
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.

Hittel answered 1/12, 2011 at 18:25 Comment(0)
S
0

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

Suzannesuzerain answered 1/12, 2011 at 18:25 Comment(0)
T
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.

Tubate answered 1/12, 2011 at 18:27 Comment(0)
C
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.

Carltoncarly answered 24/8, 2022 at 3:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.