Do Java arrays have to be initialized before they can be used?

Do Java arrays have to be initialized before they can be used?

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:

  1. public   class T {
  2.  
  3. /**
  4.  
  5. * @param args
  6.  
  7. */    
  8.  
  9. public   static   void main(String[] args) {
  10.  
  11. // TODO Auto-generated method stub  
  12.  
  13. //Define and initialize the nums array  
  14.  
  15. int nums[] = new   int []{ 3 , 13 , 4 , 6 };
  16.  
  17. //Define a prices array variable  
  18.  
  19. int prices[];
  20.  
  21. //Let the prices array point to the array referenced by nums  
  22.  
  23. prices = nums;
  24.  
  25. for ( int i= 0 ;i<prices.length;i++){
  26.  
  27. System.out.println(prices[i]);
  28.  
  29. }
  30.  
  31. //Assign the third element of the prices array to 100  
  32.  
  33. prices[ 2 ] = 100 ;
  34.  
  35. //Access the third element of the nums array and you will see array 100  
  36.  
  37. System.out.println(nums[ 2 ]);
  38.  
  39. }
  40.  
  41. }

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.

<<:  Xcodeghost ends, more than 300 popular apps infected with malicious programs, virus creator identified

>>:  A preliminary study on the method of “design that touches people’s hearts”

Recommend

Want to get rid of odor? Garlic lovers may try drinking yogurt

Science and Technology Daily, Beijing, September ...

Xue Li's "Elementary English Grammar"

: : : : : : : : : : : : : : :...

How do I attract targeted customers through Zhihu?

Hello everyone! The following tutorial can help y...

Homemade wine tastes great, naturally pure? But I urge you to be careful...

It is the grape harvest season again. Many people...

How does Keep create popular content? Explain clearly a set of operating models!

A few days ago, when I was watching TV, I found t...

2020 Short Video Live Streaming Operation and Monetization Guide

After Wangfujing Department Store and other shopp...

User Activation Methodology of AARRR Model

What is the AARRR model ? The AARRR model is also...

Data operation: How to use data analysis to achieve user growth?

What does data analytics mean for growth? How to ...

How to make a perfect Laba garlic?

"Little kids, don't be greedy, after Lab...

ASP.NET 5 & MVC6 Series (5): Configuration Information Management

In the previous chapter, we know that the new ver...

Xiaohongshu promotion strategy: Xiaohongshu live broadcast internal testing!

According to Xiaohongshu influencers, Xiaohongshu...

Nanjing 90-minute tea tasting studio [2022 highly recommended]

Nanjing tea drinking resource reservation, Nanjin...