Analysis of object array in java

 Original text: Author Han Zhiping  Education and Teaching Forum · Mid-  2012 Issue 4 


xiaogugood  My Point:

key point:

After creation, basic data type arrays can directly assign and refer to array elements; while for custom object arrays, each object element in the array needs to be created independently, and then can be assigned, referenced, etc., if Not creating each object element individually, will result in a null pointer exception


original:


Abstract: Java is an object-oriented programming language. As an important part of the basic part of javase, object array is a knowledge point that students are often confused when getting started. Combined with the author's personal teaching accumulation for many years, the basic type array and object array are analyzed. The citations are analyzed and discussed, so that students can better grasp and apply citation data types.

 
Java is an object-oriented programming language. Now it has become the mainstream language in the software development industry. In many vocational colleges and related majors, the course of java language programming has basically been opened. The storage of data types in java is divided into two categories, primitive data types (also known as primitive data types) and reference data types (also known as composite types). Arrays are one of the reference data types. It is an array of reference data types and basic data types, and is similar to reference data types when stored. At the same time, it is different from the declaration and reference of custom class object arrays. Let's take one-dimensional array as an example to discuss the basic data type array and object array respectively.

1. Array of basic data types

Arrays must be declared first, then created and used. The declaration of the basic data type array has the following formats (taking the int type as an example): ①int[]array; ②int[]array=new int; ③int[]array={1, 2, 3}; int[]array= newint[]{1, 2, 3}; The above formats operate on the contents of the array, which are divided into two forms: dynamic initialization and static initialization of the array. The dynamic assignment operation to the array in the program is shown in the following code:

public class test{

public static void main(string args[]){

int[] arrayi;

arrayi=new int[3];

/*int[]arrayi=new int[3];*/ //Or use the above format to declare and create an array

*for(int i=0; i system.out.println("\nAfter dynamic initialization:");

arrayi[0]=1; arrayi[1]=2; arrayi[2]=3; for(inti=0; i Result of the above program: 0, 0, 0; after dynamic initialization: 1, 2, 3. From The analysis of the above program results shows that in an integer array, a single array element is like an integer variable. After it is created, the value is stored in the memory, and it can directly assign values ​​to the array elements and directly reference them.

2. Array of custom objects

An array of custom objects, which are ordered by multiple concrete instances of the same class. When the string args in the familiar main method receive initialization parameters, the parameter is a typical case of an array of objects. Since each element is a reference data type, after the array is created, each object must be instantiated and created separately. Taking the custom student class student as an example, the student class is defined as follows:

class student{

private string name;

public student(string name){this.name=name;}

public string getname(){return this.name;}}

The array declaration and initialization formats of custom objects are as follows: ①student[]array; array=new student[3]; ②student[]array=new student[3]; ③student[]array=newstudent[]{new student( "lilei"), newstudent("cili"), new student("wuxi")}; student[]array={newstudent("lilei"), new student("cili"), new student("wuxi")} ;

To dynamically initialize an array of custom objects, the code is as follows:

class student{

private string name;

public student(string name){this.name=name;}

public string getname(){return this.name;}}

public class test {

public static void main(string args[]){

student array[];

array=new student[3];

/*student[]array=newstudent[3];*/

for(int i=0;i  array[0]=new student(”lilei”);

array[1]=new student(”cili”);

array[2]=new student(”wuxi”);

system.out.println("After dynamic initialization:");

for(int i=0; i The running result of the above program: null, null, null; after dynamic initialization: lilei, cili, wuxi. From the analysis of the running result of the dynamic initialization program of the object array, only the stack is established after the object array is created The address space of the memory, so the output is all null when the objects in each array are not created, and the object data can be properly initialized only after each object instance is independently opened up for the heap memory space.

3. Summary

This paper mainly analyzes the comparison program case analysis of the declaration, creation and assignment of arrays in two categories. The comparison teaching method can more highlight the differences between the two arrays, making it easier for students to understand the basic data type arrays in Java and customize them. The difference between object arrays is that, after creation, basic data type arrays can directly assign and reference array elements; while for custom object arrays, each object element in the array needs to be created independently before assigning any value to it. , references and other operations, if not created for each object element individually, will result in a null pointer exception. It is hoped that the above teaching can consolidate students' mastery of knowledge and lay a solid foundation for subsequent learning.

references:

[1]bruce eckel.think in java, third edition[m]. Beijing: Machinery Industry Press, 2004.

[2] Li Zhongwei, Ma Wenqiang, Chen Dandan, etc. Java from entry to mastery [m].2008.


Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324081929&siteId=291194637