Brief description of the production and use of Java objects

xiaoxiao2021-03-06  38

We all know that creating a new object requires the new keyword and the class name you want to create, such as: Person P1 = New Person (); // "=" The left side is defined as the variable PER as the variable type, come Pointing the instance object of a Person class created by the new keyword, the variable P1 is the reference handle of the object, and the reference handler of the object is a variable allocated in the stack. The object itself is allocated in the heap; in New Be sure to follow a pair of parentheses after the class name of the statement (), which is executed after the completion is shown in Figure 1. Stack memory reable memory | --- | ________ | | | | _______ | -> 0x3000 | Age // "0" is the object letter generated by new person () __ | | |. ....... | Number | | _______ | - |. ........ | P1 | 0x3000 | - |. ....... | Stack | --- | | | | Space Figure 1

The variable cannot be used before being initialized, and the variable inside one method must be initialized, otherwise the compilation cannot be passed. When an object is created, the initialization assignment of various types of member variables is automatically performed in Table 1, except that variable types other than the basic data type are reference types, such as Person, and previous speakers.

Table 1 Member variable type initial value ----------------------------------------- ------------------------------------------- Byte 0 Short 0 INT 0 Long 0L float 0.0f double 0.0d char '/ u0000' (expressed empty) Boolean False All Reference Type Null --------------------------- -------------------------------------------------- -------- Thus we can see the initial value of the AGE member variable in the object memory status diagram is 0.

Once you have created a new object, you can use the "Object Name. Object member" format to access the object's member (including properties and methods), and demonstrate the generation and use of the Person class object: Class Person {Int Age; Void Shout () {system.out.println (age);}} class testperson {public static void main (string [] args) {person p1 = new person (); Person P2 = new person (); p1.age = - 30; p1.SHOUT (); p2.SHOUT ();}}} program operation results: -30 0 Analysis: Two Person class objects are created in the testperson.main method, and define the object reference handle of the Person class P1 , P2, points to both objects, respectively. Next, the program calls the method and attributes of P1 and P2, P1, P2 are two fully independent objects, and the member variables defined in the class. They are individually instantiated in each object, and they will not be shared by all objects, change The AGE attribute of P1 does not affect the AGE attribute of P2. When calling an object, the member variable accessed inside the method is the member variable of this object itself, and the memory layout running on the above program is shown in Figure 2. Stack memory stack memory

| .............. | | | ____________________________________________________________________________________________________________________________________________________________________________________. Shout () {| _ of Age's access __ | ---- p2.SHOUT () {| _ in which access to AGE __ | ---- | | - | - | ________ | -> | ___ 0 ____ | AGE // P2 identifier object

figure 2

Each created object has its own lifecycle, and the object can only be used in its valid lifecycle. When there is no reference variable points to an object, this object will become garbage, and cannot be used again. The following is a specific garbage code and analysis: 1, the first case: {----- | Person p1 = new person (); | -> Person object is referenced; leaving the scope P1 failure, the Person object becomes garbage . ........ |} ----- | 2, second case: {Person P1 = new person (); ----> | p1 | -----> Person object P1 = NULL; ----> | P1 | -> NULL PERSON object becomes garbage .........} Analysis: After executing P1 = NULL; after the handle P1 has not exceeded its scope, it is still valid. However, it has been assigned empty, P1 no longer points to any object, this Person object is an orphan, no longer being referenced by any handle, becomes garbage.

3, the third case: {Person P1 = new person (); ---> p1 ----> Person object ........... | ----> P1 ---- -> Person object Person P2 = P1; ---> | ----> P2 -----> Person object ........... p1 = null; ---> p1 - -> null ......... ---> P2 ----> Person object}: After executing p1 = null; after the generated Person object does not turn into garbage, because this The object is still referenced by P2 until the P2 is invalid, and the generated Person object will become garbage.

转载请注明原文地址:https://www.9cbs.com/read-71165.html

New Post(0)