

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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


share|improve this question
possible duplicate of Size of a byte in memory - Java – Tom Aug 29 '11 at 15:19
up vote 2 down vote accepted

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.

share|improve this answer
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. – Jesper Aug 29 '11 at 21:04

Not sure what you meant by resized and merged

from the 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.

share|improve this answer
+1 So a byte in Java is fixed-size 8-Bit. JLS 4.2 – Fabian Barney Aug 29 '11 at 15:23
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. – Karl Knechtel Aug 29 '11 at 15:40
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. – darksky Aug 29 '11 at 16:09

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
share|improve this answer
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? – darksky Aug 29 '11 at 15:34
@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 – Sean Patrick Floyd Aug 29 '11 at 15:38
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) – darksky Aug 29 '11 at 15:59
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. – Sean Patrick Floyd Aug 29 '11 at 16:04

Your Answer


