Home> Java> javaTutorial> body text

How to set the default value of java array

王林
Release: 2023-05-14 14:28:06
forward
2706 people have browsed it

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]);
Copy after login

Output

0 0
Copy after login

(2) boolean array

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

Output:

false false
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!