Java
Byte Array
Initialization
Programming
Java Tutorial

How to initialize a byte array in Java

Codemia Knowledge Hub ·

Use new byte[size] for a zero-filled array, or an array literal such as {10, 20, 30} when you know the values up front. To set values afterwards, use a for loop or Arrays.fill(). Java's byte is an 8-bit signed integer ranging from -128 to 127, and byte arrays are the usual container for binary data from files, images and streams.

new byte[n]: default zero values

java
int arraySize = 10;
byte[] byteArray = new byte[arraySize];

All 10 elements start at zero.

Array literal

java
byte[] byteArray = {10, 20, 30, 40, 50};

Filling with a for loop

For a pattern or computed values:

java
1int arraySize = 5;
2byte[] byteArray = new byte[arraySize];
3for (int i = 0; i < arraySize; i++) {
4    byteArray[i] = (byte) (i * 10);
5}

Each element becomes its index multiplied by 10.

Arrays.fill() for a single value

java
1import java.util.Arrays;
2
3byte[] byteArray = new byte[5];
4Arrays.fill(byteArray, (byte) 1);

Every element is set to 1.

Streams (boxed Byte[] only)

java
1import java.util.stream.IntStream;
2
3Byte[] byteArray = IntStream.range(0, 10)
4                            .mapToObj(i -> (byte) i)
5                            .toArray(Byte[]::new);

There is no primitive ByteStream, so toArray(Byte[]::new) produces a boxed Byte[], not a byte[], so the variable is declared as Byte[]. Assigning the result to byte[] does not compile; use a for loop when you need a primitive byte[].

Comparing the methods

MethodExampleDescription
Defaultnew byte[10]All elements initialized to zero.
Literal&#123;10, 20, 30&#125;Directly initializes with specified values.
For Loopfor (int i = 0; ...) &#123; byteArray[i] = ... &#125;Allows custom patterns or manipulations.
Arrays.fillArrays.fill(byteArray, (byte) 1);Sets all elements to a specified default value.
StreamsIntStream.range(0, n)...toArray()Functional style; yields a boxed Byte[].

Multi-dimensional byte arrays

java
byte[][] multiByteArray = new byte[5][5];

A two-dimensional byte array suits grid-like data such as images.

Size, memory and bounds

  • Fixed length: an array's length is fixed once it is created.
  • Memory: byte arrays are more memory-efficient than character arrays for non-text binary data.
  • Bounds: accessing an index outside the array throws ArrayIndexOutOfBoundsException. Check indexes before use.

Byte arrays are common with FileInputStream and FileOutputStream for binary file I/O, and with socket streams in network code.


Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms