Introduction
It is believed that the little buddies who have programming basics all know the data structure of arrays. It should also be the first data structure that we come into contact with. Those who have learned C or C++ should know that arrays and pointers are closely related. What are the different features of arrays in Java without pointers?Let's talk about arrays in java.
Arrays are also a type
Array elements in arrays are unique, which you should all know, but because java is an object-oriented language and there is an inheritance relationship between classes (which is also in C++), sometimes there is a situation where there are multiple data types in an array. In fact, at the end of the day, the type of array elements is one, which is the parent class.
In java, arrays are also a data type, the parent of the array is Object, and the array itself is a reference type (basic types such as int, char, double, etc.)
For example, in java, one way to define arrays is int[], which defines an array of integers and cannot be separated from each other, so int[] is a reference type because the objects we create with it are arrays.
In the following learning of arrays, it is more helpful to understand arrays as reference types and not think with arrays of C or C++.
Array characteristics
Here are a few features of a simple and tidy array:
- An array is a collection of data
- Arrays as a reference type
- Array elements can be of either a basic type (array of basic types) or a reference type (array of reference types), but the same array can only be of the same type
- Array as an object, elements in the array as attributes of the object, and the array includes a member attribute length, length represents the length of the array
- The length of the array is determined after the creation of the array object and cannot be modified
- Array elements have subscripts, which start with 0, that is, the subscript of the first element is 0, and then the subscript of the last element is n-1, so we can access the elements of the array through the subscript of the array
Declarations and initialization of arrays
java actually supports two syntax declaration arrays:
type[] myArray; type myArray[];
For these two methods, we won't cover them much here, just to let you know, but the only way we need to remember and use is the first, which is better than the second, whether it's readability or understanding of arrays. Those who may have just learned C or C++ will see the second more intimate, from now on.Don't think about the second one anymore; (C#no longer supports the second grammar, and more and more languages no longer support it;
Array is a variable of reference type, so when it is known as a variable, it only defines a reference variable (i.e. a pointer). A buddy who has studied C or C++ must know that when a pointer is defined, it does not point to any valid memory space, and so does this reference variable.Therefore, the array cannot be used at this time and needs to be initialized.
ps: If you have learned C or C++, you can think about the relationship between reference variables and pointers, which makes learning easier.
Looking at the array initialization operation below, initialization actually allocates memory space to the elements of the array (similar to dynamic memory allocation in C++) and assigns values to each array element;
There are two ways to initialize an array:
1, Static Initialization
When initializing, you explicitly specify the initial value of each array element, and the length of the array is determined by the system.
The format is as follows:
type[] myArray; myArray = new type[] {element1, element2, element3, element4};
perhaps
type[] myArray = {element1, element2, element3, element4};
Test code:
public class MyArrayTest02 { public static void main (String[] args) { //Static Initialization Array 01 int[] arr; arr = new int[] {0, 1, 2, 3, 4}; //foreach loop for (int i : arr) { System.out.println(i); } //Static Initialization Array 02 Object[] objarr; objarr = new String[] {"I", " love", " coding!"}; //foreach loop for (Object str : objarr) { System.out.print(str); } System.out.print("\n"); } }
public class MyArrayTest03 { public static void main (String[] args) { //Static Initialization Array Simplified Version 01 int[] arr = {0, 1, 2, 3, 4}; //foreach loop for (int i : arr) { System.out.println(i); } //Static Initialization Array 02 Object[] objarr = {"I", " love", " coding!"}; //foreach loop for (Object str : objarr) { System.out.print(str); } System.out.print("\n"); } }
Either of these is possible. The second is simpler than the first, but I think the first is better for understanding the memory space of the array.
2, Dynamic Initialization
When initializing, you only need to specify the length of the array, and the system will automatically assign the initial value.
The format is as follows:
type myArray = new type[length];
Here is the initial value that the system automatically assigns:
- byte, shoet, int, long initial value is 0
- float, double initial value is 0.0
- The initial char value is'0000'
- boolean initial value is false
- Initial value of all reference types is null
Test code:
public class MyArrayTest02 { public static void main (String[] args) { //Dynamic Initialization Array int[] arr = new int[5]; for (int i = 0; i < arr.length; ++i) { arr[i] = i; } //foreach loop for (var i : arr) { System.out.println(i); } } }
Array and memory
Although there is no pointer in java, Java introduces the concept of reference, which is also an operation on memory, so understanding the underlying mechanism is particularly important for code writing and understanding.
The following is a little more difficult, but if you have touched the concept of pointer in advance, you will find many similarities for the following;
Array reference variable is a reference. Array elements can only be accessed through array variables if the reference variable points to valid memory.
Similarly, elements in an array, the array object itself, can only be accessed by reference variables in the array;
A key!!!
Array objects are stored in the stack, and reference variables are stored in the stack (when reference variables are local variables)
{This is analogous to the C language pointer which allocates memory dynamically. When a pointer allocates a piece of memory dynamically, it is stored in the heap, and this pointer variable is stored in the stack, and only through this pointer variable can the allocated dynamic memory be accessed}
Draw a picture to better understand:
Let's take a look at the case, the code will help you understand better:
public class PointTest { public static void main (String[] args) { int a[] = {1, 2, 3, 4, 5}; //Note: at this point the length of b is 6 int[] b = new int[6]; for (int i : a) System.out.print(i + " "); System.out.println(); for (int i : b) System.out.print(i + " "); System.out.println(); //b Memory pointing to a b = a; //When b points to the memory space that a points to, the length of b is the length of a System.out.println("b The length is:" + b.length); //At this point, the memory data pointed to by b is for (int i : b) System.out.print(i + " "); System.out.println(); //When the first data of a changes, output the first data of b to see if it changes a[0] = 100; System.out.println(b[0]); } }
Explain here:
Start by defining two reference variables: A and b, then system memory produces four memory areas, A and B are in the stack area, and the arrays a and B point to are themselves in the stack area;
A refers to an array object with five elements {1, 2, 3, 4, 5}, while B refers to a system-initialized data object with six elements {0, 0, 0, 0, 0}. When B points to a, the data object of B is in a situation where no reference variable is referenced, and the data object of a is referenced by two reference variables, A and b, where the length of B is a 5,So when the first data of a changes, it is equivalent to the first data change of B.
To summarize:
When we look at an array, we have to think of it as two parts, one as a reference to the array, that is, the array reference variable defined in the code (stored on the stack) and the other as the actual array object (stored on the stack);So you need to use referenced variables to access array objects;
summary
Here I'm just going to give you a brief introduction to arrays in java. Don't think that if you really learn arrays, they can only be regarded as a starting point for Java arrays. You may be able to learn how to use arrays, but there are still many things that are hard to understand about how arrays work in memory. It is strongly recommended that you learn C pointers in advance.It is also a good idea to understand pointer arrays, which will help you better understand the storage and initialization of arrays in memory, understand these, and you will have a more accurate understanding of how your program works!
ps: Content welcome to communicate if you have any questions!!