简体   繁体   中英

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

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 );

只需将其创建为new byte[100] ,默认情况下将初始化为0

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];

使用默认值初始化所有字节元素,对于字节为0,实际上,构造时数组的所有元素都使用数组元素类型的默认值进行初始化。

任何基元数组的默认元素值已经为零:布尔false

实际上byte的默认值是0。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM