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?
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[] .
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.
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.
Yes, primitives are light-weight compared to corresponding Wrapper class objects.
You can read about it here : Primitives vs Wrappers
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With