Java Object array

Source: Internet
Author: User

Key points:

After creation, an array of basic data types can directly assign values, references, and so on, while a custom array of objects needs to be created independently of each object element in the array before it can be assigned a value, a reference, and so on, which causes null pointer exceptions if not created individually for each object element

Original:

Abstract: Java is an object-oriented programming language, an object array as an important part of Javase, is the students in the introduction of often confusing knowledge points, combined with the author's personal years of teaching accumulation, the basic types of arrays and object arrays of reference analysis and discussion, Enable students to better master and apply reference data types.


Java is an object-oriented programming language, has now become the mainstream language in the software development industry, in many higher vocational colleges computer and related majors have already opened the Java language Program design this course. The storage of data types in Java is divided into two categories, basic data types (also known as raw data types) and reference data types (also known as composite types). An array is one of the reference data types. It is an array of reference data types, basic data types, and is similar to the reference data type when stored, and it differs from the Declaration and reference of a custom class object array. The following is an example of a one-dimensional array, an array of basic data types and an array of objects are discussed separately.

An array of basic data types

Arrays are declared before they are created and used. The declaration of an array of basic data types is in the following formats (for example, type int): ①int[]array;②int[]array=new int;③int[]array={1,2,3};int[]array=newint[]{1,2,3} In the above several formats, the array content operation is divided into two forms: dynamic initialization and static initialization of the logarithm group. The dynamic assignment operation of an array in a program, as 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 declaring and creating arrays in the above format

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

Arrayi[0]=1;arrayi[1]=2;arrayi[2]=3;for (Inti=0;i The above program operation result: 0, 0, 0; After dynamic initialization: 1, 2, 3. From the above program results analysis, in an integer array, a single array element is like an integer variable, the created value is stored in memory, you can directly assign values to the array element, directly to its reference operation.

Ii. Array of Custom objects

A custom array of objects that consists of a number of concrete instances of the same class. When the string args in the main method we are familiar with receives the initialization parameter, the parameter is a typical object array case. Since each of its elements is a reference data type, after the array is created, it must be created separately for each object. To customize 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 declarations and initialization formats for 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")};

For a custom object array dynamic initialization operation, 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 object array dynamic initialization program run results analysis, the object array has been created only the stack memory address space, so the object in each array is not created when the output is all null, only after each object instance of the independent development of heap memory space, to properly initialize the object data.

Iii. Summary

In this paper, the Declaration, creation and assignment of the main logarithm group are divided into two categories to carry out the comparison procedure case analysis, the contrast teaching method can highlight the difference between the two arrays, make it easier for students. The difference between an array of basic data types in Java and a custom object array is that, after creation, An array of basic data types can be used to assign values, references, and so on directly to array elements, whereas custom object arrays need to be created independently of each object element in the array before they can be assigned values, references, and so on, which results in NULL pointer exceptions if not created individually for each object element. Hope that through the above teaching can strengthen the students grasp of knowledge, for follow-up learning to tamp the foundation.

Java Object array

Related Article

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.