How to initailize byte array of 100 bytes in java with all 0's
JavaJava Problem Overview
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
Java Solutions
Solution 1 - Java
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 );
Solution 2 - Java
Simply create it as new byte[100]
it will be initialized with 0 by default
Solution 3 - Java
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
Solution 4 - Java
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.
Solution 5 - Java
The default element value of any array of primitives is already zero: false
for booleans.
Solution 6 - Java
Actually the default value of byte is 0.