An array is a composite structure provided by most programming languages. If a program requires multiple variables of the same type, you can consider defining an array. The array variable of the Java language is a reference type variable, so it has unique characteristics of Java. In normal Java development, before using Java arrays, we will initialize the arrays, allocate memory space for the elements in the array, assign values, etc., but is it necessary to initialize Java arrays? Is it okay not to initialize? In fact, Java array variables are reference type variables, not array objects themselves. As long as the array variable points to a valid array object, the array variable can be used in the program, such as the following example:
From the above code, we can see that after defining the prices array, the prices array is not initialized. When int prices[] is executed, The memory allocation of the program is shown in the figure As can be seen from the figure, at this time the prices array does not point to any valid memory, does not point to any array object, and the program cannot use the prices array variable at this time. After the program executes prices = nums, the prices variable will point to the array referenced by the nums variable. At this time, the prices variable and the nums variable reference the same array object. After executing this statement, the prices variable already points to valid memory and an array object of length 4, so the program can use the prices variable normally. For array variables, it does not need so-called initialization. As long as the array variable points to a valid array object, the program can use the array variable normally. Therefore, arrays in Java language can be used normally without initialization. Java beginners can try to use the method in the above example to deepen their impression. ***, I hope my sharing is helpful to everyone. |
>>: A preliminary study on the method of “design that touches people’s hearts”
"High-salary overseas job openings" and...
October 16th of every year is World Spine Day bec...
Beijing time, November 4th morning news, Broadcom...
The cute giant pandas are loved by people. They a...
Braised pork ribs for 15 yuan, fried kelp shreds ...
Based on my own knowledge and experience in Inter...
So what kind of influencers have more long-term a...
Author: Huang Xianghong Duan Yuechu In mankind...
Where there is traffic , there are advertisements...
Today I will share with you the most comprehensiv...
Young looks at the stars, chasing the stars and p...
Earlier, due to the development of the domestic l...
With the influence of national policies, the spee...
When I was making my first iOS app, I encountered...
[[142117]] Life depends on acting. Since August 1...