Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Byte/byte array space efficiency comparison

I need to create a space efficient 2D array for a large number of 8 bit values. I began writing my class using a few layers of abstraction and generics to allow for code reuse. Once I got to implementing the concrete class it occurred to me I cannot pass in a primitive type as a generic class argument and I would have to use a wrapper class. Because I am concerned about space efficiency, I need to know: what is the space efficiency difference between a Byte array using the wrapper class compared to a primitive byte array?

like image 651
recursion.ninja Avatar asked Apr 03 '13 13:04

recursion.ninja


People also ask

What is the difference between ByteBuffer and byte array?

The byte[] is just a primitive array, just containing the raw data. So, it does not have convenient methods for building or manipulating the content. A ByteBuffer is more like a builder. It creates a byte[] .

Is Bytearray a buffer?

Byte buffers can be created either by allocation , which allocates space for the buffer's content, or by wrapping an existing byte array into a buffer.

Should I use byte instead of int?

Generally, there's no point to make a single variable a byte instead of an int - it only matters when you have a big array of bytes instead of ints.


1 Answers

Yes, primitives are light-weight compared to corresponding Wrapper class objects.

You can read about it here : Primitives vs Wrappers

like image 104
IndoKnight Avatar answered Sep 21 '22 02:09

IndoKnight