A brief introduction to the generation and use of Java objects

Source: Internet
Author: User
Tags empty variables reference variable type null valid
Java objects We all know that creating a new object requires the use of the newer keyword and the class name of the object you want to create, such as:
person P1 = new person (); The left side of "=" defines a variable P1 with the class name person as the variable type. To point to the right of "=" The instance object of a person class created by the new keyword, the variable p1 is the object's reference handle, the object's reference handle is a variable allocated in the Stack, and the object itself is allocated in the heap ; After the class name of the new statement, be sure to follow a pair of parentheses (), which executes the memory state after the completion of the statement, as shown in Figure 1.
Stack memory heap Memory
Some |---|_______| |________|
A | |_______| -> 0x3000|____0___|age//"0" is the object created by new person ()
Letter __| |_______| - | . ....... |
number | |_______| - | . ........ |
of | p1|0x3000 | |. ....... |
Stack |---| | |
Empty
Room Figure 1



A variable cannot be used until it is initialized, and a variable inside a method must be initialized to be assigned, otherwise the compilation cannot pass. When an object is created, the various types of member variables are automatically initialized by table 1, except for the types of variables outside the base data type, such as the person above and the preceding array.

Table 1
Member Variable type initial value
--------------------------------------------------------------------------------------
BYTE 0
Short 0
int 0
Long 0L
Float 0.0F
Double 0.0D
Char ' \u0000 ' (represented as empty)
Boolean False
All reference type Null
---------------------------------------------------------------------------------------
From this we can see that the initial value of the age member variable in the Object memory state graph is 0.

After you create a new object, you can use the format of the object name. Object member to access the members of the object, including properties and methods, and the following shows how the Person class object is produced and used:
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 Run Result:
-30
0
Analysis: An object of two person classes was created in the Testperson.main method, and the object reference handle P1, p2, of the man class were defined, pointing to both objects. Then, the program calls the methods and properties of P1 and P2, p1, p2 are two completely independent objects, the member variables defined in the class are instantiated individually, not shared by all objects, changing the age attribute of P1, and not affecting the age attribute of P2. When a method of an object is invoked, the member variable that is accessed inside the method is a member variable of the object itself, and the memory layout of the above program runs as shown in Figure 2.

Stack memory heap Memory

| . ................ | |________|
| . | Objects identified by the-----> |__-30 ___|age//p1
|___________________| -
P1.shout () {|_ the access to age in __|----
P2.shout () {|_ the access to age in __|----
| | -
| | - |________|
Objects identified by the-----> |___0____|age//p2


Figure 2


Each created object has its own lifecycle, the object can only be used within its valid lifecycle, and when no reference variable points to an object, the object becomes garbage and can no longer be used. The following are specific spam codes and analyses:

1, the first situation:
{ -----|
Person P1 = The new Person (), |--> the person object is referenced, leaving the scope P1 invalid, and the person object becomes garbage.
........ |
} -----|

2, the second situation:
{
person P1 = new person (); ---->| P1 | -----> Person Objects
P1 = null; ---->| P1 |->null Person object becomes garbage
.........
}
Analysis: After executing p1=null, even if the handle P1 has not exceeded its scope, still valid, but it has been assigned null, p1 no longer point to any object, this
The person object becomes an orphan and is no longer referenced by any handle and becomes rubbish.

3. The third situation:
{
person P1 = new person (),---> P1----> Person Object
........... | ----> P1-----> Person Object
person P2 = p1; --->|----> P2-----> Person Object
...........
P1 = null; ---> p1----> Null
, ...---> p2----> Person object.
}
Analysis: After P1=null is executed, the resulting person object does not become garbage because the object is still referenced by P2 until P2 is out of scope and is invalid, resulting
Person object will become garbage.





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.