5

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

Thanks,

Tom
  • 43,810
  • 29
  • 138
  • 169
darksky
  • 20,411
  • 61
  • 165
  • 254
  • possible duplicate of [Size of a byte in memory - Java](http://stackoverflow.com/questions/229886/size-of-a-byte-in-memory-java) – Tom Aug 29 '11 at 15:19

3 Answers3

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.

TS-
  • 4,311
  • 8
  • 40
  • 52
  • +1 So a byte in Java is fixed-size 8-Bit. [JLS 4.2](http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html) – 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
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
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • 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](http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html). 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
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.

Jord Sonneveld
  • 456
  • 2
  • 4
  • 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