Best practice for getting datatype size(sizeof) in Java

前端 未结 7 1708
感情败类
感情败类 2021-02-01 03:53

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*size         


        
7条回答
  •  無奈伤痛
    2021-02-01 04:11

    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

提交回复
热议问题