Basic explanation of the Java array

Source: Internet
Author: User

Since the Java array covers more content, the main content is explained from a personal point of view.

If there is any shortage, please criticize

1) Is the Java array dynamic or static?

The Java language is a typical static language, which infers that the Java array is also static. What is static means that the Java array must determine the size of the space allocated in memory when it is initialized.

The implementation is done by initializing the array (note that the two concepts do not mix when declaring an array), specifying the length of the array, or specifying the contents of the initialized array.

It is also necessary to understand that the array variable is not the array itself, the array variables are stored in the stack, and the corresponding array objects are stored in the heap. The array variable in the stack points to the array object in the heap.

For example:

Package Com.yonyou.test;import java.util.hashset;import Java.util.iterator;import java.util.set;/** * Test class * @author Little Hao * @ Created Date 2015-3-2 */public class test{public  static void Main (string[] args) {  int[] array=new int[10];//when initializing an array The length of the class array  int[] array_new=new int[]{1,2,3};//specifies the corresponding related element when initializing an array                                                   //Note that the corresponding length can no longer be specified here. Oh                                                   //Otherwise it will be an error. System.out.println ("The length of the array one is:" +array.length); System.out.println ("The length of the array two is:" +array_new.length);}   }

  2) Default initialization values for Java arrays

A. Basic data type Byte,short,int,long, the default initialization value is 0;

B. The default initial value for basic data type float,double is 0.0

C. The default initial value of the basic data type char is: \0000

D. Default initial value of a base reference type is NULL

Package Com.yonyou.test;import java.util.hashset;import Java.util.iterator;import java.util.set;/** * Test class * @author Little Hao *  @ Created Date 2015-3-2 */public class test{public static void Main (string[] args) {short[] array_0=new short[10];  Int[] Array_1=new int[10];  Long[] Array_2=new long[10];  Byte[] Array_3=new byte[10];  Char[] Array_4=new char[10];  Boolean[] Array_5=new boolean[10];  Float[] Array_6=new float[10];  Double[] Array_7=new double[10];  Car[] Array_8=new car[10];  System.out.println ("Short:" +array_0[5]);  SYSTEM.OUT.PRINTLN ("int:" +array_1[5]);    System.out.println ("Long:" +array_2[5]);    System.out.println ("Byte:" +array_3[5]); SYSTEM.OUT.PRINTLN ("Char default value is \\u0000:" + (array_4[5]== ' \u0000 '));//cannot directly display the contents of the character/    /Indirect Display System.out.println ("Boolean:" +array_5[5]);    System.out.println ("float:" +array_6[5]);    System.out.println ("Double:" +array_7[5]);      SYSTEM.OUT.PRINTLN ("Reference class variable initialization value is::" +array_8[5]); }/** * Create aTest Object * @author Little Hao * @ creation Date 2015-3-19 */class car{}} 

  3) must the Java array be initialized?

No, it's not. For specific counter examples, see the following:

Package com.yonyou.test;/** * Test class * @author Little Hao * @ Creation date 2015-3-2 */public class test{public  static void Main (string[] args) {       car[] car1=new car[10];       Car[] CAR2;       CAR2=CAR1;       System.out.println (car2.length);         }    /**   * Create a Test object   * @author Little hao   * @ Creation Date 2015-3-19 */  class car{      }}

  4) How are the Java array initialized?

The initialization of an array is generally divided into two types:

One for static initialization:

The length of the array is specified when the array is created, as in the following:

Int[] Array_static=new int[]{1,2,3};

The second type is dynamic initialization:

Only specify the length of the array when creating the array

Int[] Array_dynamic=new int[10];

Specific application examples are as follows:

Package com.yonyou.test;/** * Test class * @author Little Hao * @ Creation date 2015-3-2 */public class test{public  static void Main (string[] args) {  //static initialization      int[] array_static=new int[]{1,2,3};      Dynamic initialization      int[] array_dynamic=new int[10];         }

  4) What is the multidimensional array in Java?

In the case of a home, there is no concept of multidimensional arrays in Java. Multidimensional arrays in Java can be treated as one-dimensional arrays. When initializing a multidimensional array, it can be converted into a corresponding one-dimensional array.

Only then the one-dimensional array is not stored in the specific element value, but the corresponding array of reference address. Specifically, not much to say, please take a look at the following example:

Package com.yonyou.test;/** * Test class * @author Little Hao * @ Creation date 2015-3-2 */public class test{public  static void Main (string[] args) {          object[] obj1=new object[4];        Obj1[2]=new object[2];        Object[] obj2= (object[]) obj1[2];        Obj2[1]=new object[3];        Object[] obj3= (object[]) obj2[1];        Obj3[2]=new int[5];        Int[] obj4= (int[]) obj3[2];        obj4[3]=100;        System.out.println (OBJ4);        System.out.println ((  int[) ((object[]) ((object[]) obj1[2]) [1]) [2]);        System.out.println (Obj4[3]);        System.out.println ((int[) ((object[]) ((object[]) obj1[2]) [1]) [2]) [3]);}  }  

  

All right, let's get here first.

Basic explanation of the Java array

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.