java - 如何在java中用全0初始化100字节的字节数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16475457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:03:59  来源:igfitidea点击:

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

java

提问by user1393608

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中用全0初始化100字节的字节数组。我想创建 100 字节的数组并用全 0 初始化它

采纳答案by wolfcastle

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

使用其他值初始化的更通用方法是使用Arrays类。

import java.util.Arrays;

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

回答by Evgeniy Dorofeev

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

只需创建它,因为new byte[100]默认情况下它会被初始化为 0

回答by Sergii Shevchyk

byte [] arr = new byte[100] 

Each element has 0 by default.

每个元素默认为 0。

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

回答by c.P.u1

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.

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

回答by Liping Huang

Actually the default value of byte is 0.

实际上,byte 的默认值是 0。

回答by user207421

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

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