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?

1
  • The JLS specifies the size of primitive types exactly. (but I'm not seeing the size of a boolean in there)
    – Atreys
    Commented Jul 20, 2011 at 18:41

7 Answers 7

49

See @Frank Kusters' answer, below!

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

3
  • 1
    I put in an enhancement suggestion for Guava to make this a little less verbose.
    – Ed Staub
    Commented Jul 21, 2011 at 15:14
  • 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 Staub
    – Pie
    Commented Feb 14, 2020 at 2:35
  • @Pie No. The constant is provided by the wrapper class, but it is the size of the primitive.
    – Ed Staub
    Commented Feb 15, 2020 at 3:13
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

1
  • This is simpler than the accepted answer and should be the new accepted answer IMO
    – Matt Corby
    Commented Sep 18, 2019 at 13:02
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);
3
  • suggestion: use switch case for better readability, java 7 supports strings in switch statements.
    – thoughtbot
    Commented May 16, 2014 at 3:00
  • 11
    I think the readability is great. Using Strings would ruin performance. Commented May 16, 2014 at 12:58
  • this is not correct: the classes have overhead compared to the primitive types. An Integer will required more memory than the equivalent int.
    – ChrisBlom
    Commented Feb 16 at 8:06
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.

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.

4
  • With the focus on NIO.2 in JDK 7. This answer is the best practice.
    – Sym-Sym
    Commented Apr 29, 2014 at 22:55
  • 1
    Not if you are using NIO to interface with other APIs such as OpenGL which still require number of bytes (not numbers).
    – mlepage
    Commented Jul 10, 2014 at 3:26
  • What is the size in java?
    – Joehot200
    Commented May 11, 2015 at 15:52
  • @Joehot200 when you create a ByteBuffer you have to give it a size in byte. Commented May 11, 2015 at 16:56
1

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

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.