• Post Reply Bookmark Topic Watch Topic
  • New Topic

Implementing sizeof in java?  RSS feed

 
schindler oscar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

How to implement sizeof functionality in java similar to C or C++?
Is there any way to do it?

Please provide me your inputs.

Thanks.
 
balamurugan c v
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 programmer-perceivable width of this data type. Such an int is essentially an abstract data type and can be backed up by, say, a 64-bit physical memory word on a 64-bit machine.
The same goes for nonprimitive types: the Java language specification says nothing about how class fields should be aligned in physical memory or that an array of booleans couldn't be implemented as a compact bitvector inside the JVM.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you think you need to know this?
 
faisal usmani
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ,


That is a co-incidence a month back , I posted the same topic . It seems you too are from C/C++ background . I got replied by Sir Ernest Friedman-Hill who is sheriff and author of the ranch his member id is 52711
I am just pasting what he replied to me , Hope it is of use to you also




*************************************************************************

The actual amount of memory that an object uses depends on the Java implementation -- i.e., it's not specified by the Java VM specification or the Java Language specification. The number is never directly useful in any program -- no Java language function or construct depends on the memory image size of an object. The precise answer may well vary by JVM vendor, by platform, and even by version.

That said, for Sun's JVMs a good rule of thumb is 16 bytes for an object, plus four bytes for each member -- except doubles and longs, which are 8 bytes each.
**************************************************************************


Cheers
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!