[java] How to initailize byte array of 100 bytes in java with all 0's

How to initialize byte array of 100 bytes in java with all 0's. I want to create 100 byte array and initialize it with all 0's

This question is related to java

The answer is


A new byte array will automatically be initialized with all zeroes. You don't have to do anything.

The more general approach to initializing with other values, is to use the Arrays class.

import java.util.Arrays;

byte[] bytes = new byte[100];
Arrays.fill( bytes, (byte) 1 );

Simply create it as new byte[100] it will be initialized with 0 by default


byte [] arr = new byte[100] 

Each element has 0 by default.

You could find primitive default values here:

Data Type   Default Value
byte        0
short       0
int         0
long        0L
float       0.0f
double      0.0d
char        '\u0000'
boolean     false

byte[] bytes = new byte[100];

Initializes all byte elements with default values, which for byte is 0. In fact, all elements of an array when constructed, are initialized with default values for the array element's type.


The default element value of any array of primitives is already zero: false for booleans.


Actually the default value of byte is 0.