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

Java

Java 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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1393608View Question on Stackoverflow
Solution 1 - JavawolfcastleView Answer on Stackoverflow
Solution 2 - JavaEvgeniy DorofeevView Answer on Stackoverflow
Solution 3 - JavaSergii ShevchykView Answer on Stackoverflow
Solution 4 - Javac.P.u1View Answer on Stackoverflow
Solution 5 - Javauser207421View Answer on Stackoverflow
Solution 6 - JavaLiping HuangView Answer on Stackoverflow