Best practice for getting datatype size(sizeof) in Java

Wei Shi picture Wei Shi · Jul 20, 2011 · Viewed 62k times · Source

I want store a list of doubles and ints to a ByteBuffer, which asks for a size to allocate. I'd like to write something like C's syntax

int size=numDouble*sizeof(double)+numInt*sizeof(int);

But there is no sizeof in Java. What is the best practice to calculate the size in byte? Should I hardcode it?

Answer

Ed Staub picture Ed Staub · Jul 21, 2011

See @Frank Kusters' answer, below!

(My original answer here was for Java versions < 8.)