Best practice for getting datatype size(sizeof) in Java
Asked Answered
H

7

31

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?

Hyalite answered 20/7, 2011 at 18:3 Comment(1)
The JLS specifies the size of primitive types exactly. (but I'm not seeing the size of a boolean in there)Cordage
V
49

See @Frank Kusters' answer, below!

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

Veterinary answered 21/7, 2011 at 6:34 Comment(3)
I put in an enhancement suggestion for Guava to make this a little less verbose.Veterinary
So there is no way to find the size of primitive data types, but we can find the size of the wrapper classes, Is it correct? @Ed StaubOsteen
@Osteen No. The constant is provided by the wrapper class, but it is the size of the primitive.Veterinary
H
39

Since Java 8, all wrapper classes of primitive types (except Boolean) have a BYTES field. So in your case:

int size = numDouble * Double.BYTES + numInt * Integer.BYTES;

Documentation: http://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html

Hamstring answered 18/7, 2014 at 12:19 Comment(1)
This is simpler than the accepted answer and should be the new accepted answer IMOTerritorialism
S
9

Write your own method. In Java the datatypes are platform independent always the same size:

public static int sizeof(Class dataType)
{
    if (dataType == null) throw new NullPointerException();

    if (dataType == int.class    || dataType == Integer.class)   return 4;
    if (dataType == short.class  || dataType == Short.class)     return 2;
    if (dataType == byte.class   || dataType == Byte.class)      return 1;
    if (dataType == char.class   || dataType == Character.class) return 2;
    if (dataType == long.class   || dataType == Long.class)      return 8;
    if (dataType == float.class  || dataType == Float.class)     return 4;
    if (dataType == double.class || dataType == Double.class)    return 8;

    return 4; // 32-bit memory pointer... 
              // (I'm not sure how this works on a 64-bit OS)
}

Usage:

int size = numDouble * sizeof(double.class) + numInt * sizeof(int.class);
Stevenson answered 20/7, 2011 at 18:8 Comment(3)
suggestion: use switch case for better readability, java 7 supports strings in switch statements.Meiny
I think the readability is great. Using Strings would ruin performance.Stevenson
this is not correct: the classes have overhead compared to the primitive types. An Integer will required more memory than the equivalent int.Disembogue
M
5

A better solution might be to not emulate C syntax and use an ObjectOutputStream with a nested ByteArrayOutputStream to generate a byte array which can then be written to your ByteBuffer.

Maisiemaison answered 20/7, 2011 at 18:27 Comment(0)
B
3

The size in Java is always the same. You can hardcode it but you only need to do this because you are working with bytes in a ByteBuffer. If you use double[] or DoubleBuffer you don't need these.

Barta answered 20/7, 2011 at 18:7 Comment(4)
With the focus on NIO.2 in JDK 7. This answer is the best practice.Puggree
Not if you are using NIO to interface with other APIs such as OpenGL which still require number of bytes (not numbers).Overtask
What is the size in java?Cession
@Cession when you create a ByteBuffer you have to give it a size in byte.Barta
K
1

You can also use the sizeof4j library to get the sizeof the double you just need SizeOf.doubleSize()

Kinfolk answered 3/12, 2014 at 16:36 Comment(0)
I
0

Automated and abstract solution is to write the sample to DataOutput and see the resulted size.

Impregnable answered 8/1, 2020 at 13:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.