


1. Arrays and array elements
Arrays in Java are reference types
The elements of an array can be basic types or references Type, clarifying the type of array elements can help us understand the default initialization of array elements
2. One-dimensional array
The default initialization value of one-dimensional array elements is divided into There are two types, the elements are basic data types and reference data types
1. When the elements are basic data types
- ##Integer types (byte, short, int, long ) The default initial value is 0
- Floating point type (float, double) The default initial value is 0.0
- Boolean type (boolean) The default initial value is flase
- Character type (char) The default initial value is 0 (null character)
public class Test {
public static void main(String[] args) {
char[] a = new char[2];
if(a[0] == 0) {
System.out.println("这是判断0的" + a[0] + "测试!");
}
if(a[0] == '0') {
System.out.println("这是判断字符'0'的" + a[0] + "测试!");
}
}
}
Through the results we found The value of a[0] is judged to be 0 instead of the character '0'. When printed, a[0] is actually a null character (this is not a space!!!)
2. The array elements are reference types When
the element is a reference type, the default initial value is null
3. Two-dimensional array
A two-dimensional array in Java actually uses a one-dimensional array as an element of the array The interpretation of the default initialization value of the two-dimensional array composed of
is divided into two situations (the writing format of the following two situations is represented by int, and int can be replaced by other data types)
1 . int[][] arr = new int[2][2]
- ①Outer element (arr[0], arr[1]): address value
- ②Inner element (arr[0][0]): Use the default initial value of the one-dimensional array to determine
- 2. int[][] arr = new int[2][]
- ①Outer element (arr[0], arr[1]): address value
- ②Inner element (arr[0][0]): Null pointer exception
- We understand it with the following figure, because the two-dimensional array is actually an array of arrays, and the outer layer saves is the address value, the inner layer can naturally judge based on the one-dimensional array
The above is the detailed content of Explanation on how to determine the default initialization value of Java one-dimensional array and two-dimensional array elements. For more information, please follow other related articles on the PHP Chinese website!

Start Spring using IntelliJIDEAUltimate version...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Java...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version
Chinese version, very easy to use

Atom editor mac version download
The most popular open source editor