What does it mean to declare an array in Java
This article mainly shows you "what is the meaning of declarative array in Java". It is easy to understand and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "what is the meaning of declarative array in Java".
Declaring an array in Java means declaring the name of the array and the data type of the elements contained in the array. The array in the Java program must be initialized before it can be used. The so-called initialization means allocating memory space for the elements of the array object and specifying the initial value for each array element. In Java, the array is static, and once the array is initialized, the length has been determined and can no longer be changed at will.
Declare array variables
You must declare array variables before you can use arrays in your program. The following is the syntax for declaring array variables:
Preferred method for dataType [] arrayRefVar;//
Or
DataTypearrayRefVar []; / / the effect is the same, but it is not the preferred method to follow C++.
However, the above only declares the array and cannot be used directly, because no memory space is allocated for it and must be initialized at this time.
For example, the following code will report an error in Java8
Publicclasstest {
Publicstaticvoidmain (String [] arg) {
Int [] a
A [0] = 1
System.out.println (a [0])
}
}
/ / the system will report an error. The local variable a may not be initialized.
/ / to further add my personal understanding, the declared array does not have a fixed size, there is no way to allocate memory space, all must be initialized
/ / but the following code is prohibited in Java
Publicclasstest {
Publicstaticvoidmain (String [] arg) {
Int [10] a
A [0] = 1
System.out.println (a [0])
}
}
/ / the system will report that there is a syntax error on the error mark "10". Delete this flag
/ / an array cannot be initialized like C++
Declaration of one-dimensional array
There are two syntax formats for declaring an one-dimensional array:
Array element type array name []
Array element type [] array name
Its square bracket [] is the flag of the array, it can appear after the array name or after the array element type name, there is no difference between the two methods of definition. The following example declares an array of integers:
IntintArray []
Equivalent to
Int [] intArray
Declaration of two-dimensional array
The format is:
Type array name [] []
Like an one-dimensional array, you need to use the new keyword to allocate memory space for a two-dimensional array.
The above is all the content of the article "what is the meaning of declaring arrays in Java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!