5

If I create an array of bytes with byte[], what would be the size of each element? Can they be resized/merged?

Thanks,

1

3 Answers 3

7

Not sure what you meant by resized and merged

from the documentation:

byte: The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). The byte data type can be useful for saving memory in large arrays, where the memory savings actually matters. They can also be used in place of int where their limits help to clarify your code; the fact that a variable's range is limited can serve as a form of documentation.

Edit: If by resized/merged you are talking about the array itself, there's nothing special about a byte array compared to other arrays.

Sign up to request clarification or add additional context in comments.

3 Comments

+1 So a byte in Java is fixed-size 8-Bit. JLS 4.2
I assume he wants to be able to, e.g., treat the byte[] as an int[] by treating sets of 4 consecutive bytes as ints.
I need to serialize objects and store them as bytes in an array. I'm not sure howI can know which elements will occupy what object since each element only takes 1 byte and the stored objects can go up to 100 bytes.
6

There are two ways to allocate an array.

A) allocate an empty array of a given size:

byte[] ba1 = new byte[18]; // 18 elements

B) allocate an array by specifying the contents

byte[] ba2 = {1,2,3,4,5}; // 5 elements

4 Comments

For byte[] ba2 = {1,2,3,4,5}; // 5 elements, does it convert the int 1 to bites and store it in ba2[0]? And the same for the rest of the elements? Can I store actual bits in there, so like 10010101?
@Nayefc see Primitive Data types. Also, starting from JDK7, there is a byte literal: byte b = 0b10010101. In previous versions, there were only literals for decimal, octal and hexadecimal values
Yes I know that.. But in your example, if you're assigning "1", "2" and "3" into a byte array, would it convert the number to byte and store it? It can't store the int "1" in a byte array as int is 32-bits (which is 4 bytes)
would it convert the number to byte and store it No, the compiler knows from the context that this is a byte literal. which means that byte[] s = { 127 } is legal, while byte[] s = { 128 } is not.
4

The size would be a byte per element.

They can not be re-sized. However you can merge them yourself using System.arrayCopy() by creating a new array and copying your source arrays into the new array.

Edit 1:

There is also an 8-byte overhead for the object header and a 4-byte overhead for the array length, for a total overhead of 12 bytes. So small arrays are relatively expensive.

Check out GNU Trove and Fastutil. They are libraries that make working with primitive collections easier.

Edit 2:

I read in one of your response that you're doing object serialization. You might be interested in ByteBuffers. Those make it easy to write out various primitive types to a wrapped array and get the resulting array. Also check out Google protocol buffers if you want easily serialized structured data types.

1 Comment

Note that the mentioned overheads are JVM implementation dependent. One JVM implementation or version might have different overheads than another. Do not write a program that relies on these numbers, because it will not work on all JVMs.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.