Java array does not have to be initialized or used properly

Source: Internet
Author: User

Java array does not have to be initialized or used properly

Arrays are a composite structure provided by most programming languages. If a program requires multiple variables of the same type, you can consider defining an array. Array variables of the Java language are referenced type variables, so they have unique features of Java.

In normal Java development, we will initialize the array before using the Java array to allocate memory space and assign values to the elements in the array. But must the Java array be initialized? Can I disable initialization?

In fact, the array variable of java is a reference type variable, not an array object itself. As long as the array variable points to a valid array object, this array variable can be used in the program, for example:

Public class T {

/**

* @ Param args

*/

Public static void main (String [] args ){

// TODO Auto-generated method stub

// Define and initialize the nums Array

Int nums [] = new int [] {3, 13, 4, 6 };

// Define a prices array variable

Int prices [];

// Point the prices array to the array referenced by nums

Prices = nums;

For (int I = 0; I <prices. length; I ++ ){

System. out. println (prices [I]);

}

// Assign the third element of the prices array to 100

Prices [2] = 100;

// Access the third element of the nums array and you will see array 100

System. out. println (nums [2]);

}

}

The code above shows that the prices array is not initialized after the prices array is defined. After int prices [] is executed,

Program memory allocation

As shown in the figure, the prices array does not point to any valid memory or any array object. At this time, the program cannot use the prices array variable.

After the program executes prices = nums, the prices variable points to the array referenced by the nums variable. In this case, the prices variable and the nums variable reference the same Array object.

After executing this statement, the prices variable has been directed to the valid memory and an array object with a length of 4, so the program can use the prices variable normally.

For an array variable, it does not need to be initialized. As long as the array variable points to a valid array object, the program can use the array variable normally.

Therefore, arrays in Java do not have to be initialized or used properly. Java beginners can try to use the method shown in the above example to gain a deeper impression. Finally, I hope that the sharing of the small series will be helpful to you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.