Home>Article>Java> How to set the default value of java array

How to set the default value of java array

王林
王林 forward
2023-05-14 14:28:06 2699browse

1. Default values of different arrays

For arrays defined by int type, the initial default value is 0.

Array defined by String type, the default value is null.

The result of defining a char type array using the UTF8 character set is 0.

Array defined by double type, the default value is 0.0.

Array defined by float type, the default value is 0.0.

Array defined by boolean type, the default value is false.

2. Example

(1) int array:

int[] ia = new int[2]; System.out.println(ia[0]); System.out.println(ia[1]);

Output

0 0

(2) boolean array

boolean[] ba = new boolean[2]; System.out.println(ba[0]); System.out.println(ba[1]);

Output:

false false

When some friends were learning arrays, they discovered that arrays also have default values. However, there are many types of arrays in Java, so the default values of different types of arrays are also different.

The above is the detailed content of How to set the default value of java array. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete