Get the App
SLTechnology News&Howtos  ›  Development  › 

Does the Java array have to be initialized before it can be used

Shulou Source: shulou.com Published: 2022-06-02 08:09:04 09月23日 Update

Editor to share with you whether the Java array must be initialized before it can be used, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Array is a compound structure provided by most programming languages, and if the program needs multiple variables of the same type, you can consider defining an array. The array variables of the Java language are variables of reference type, so they have the unique characteristics of Java.

In normal Java development, before using the Java array, we all initialize the array, allocating memory space, assigning values, etc., to the elements in the array, but does the Java array have to be initialized? Is it okay not to initialize?

In fact, the array variable of java is a variable of reference type, not the array object itself. 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:

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 [] {3pm 13pm 4pm 6}; / define a prices array variable int prices []; / / point the prices array to the array prices = nums referenced by nums For (int iTuno Bandi)

Tags: Array variable point program object valid memory element article type language three content allocation same not much code example multiple most Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Microsoft OPPO Reno Redmi macOS