Java alias Mechanism

Source: Internet
Author: User

Aliases mainly occur when values are assigned:
Assigning values to basic data types is simple. the basic data type stores actual values instead of references to an object. Therefore, when assigning values to an object, the content in one place is directly copied to another place. for example, if a = B is used for the basic data type, the content of B is copied to. if a is modified, B is not affected by the modification.
However, when assigning values to an object, the situation changes. when performing operations on an object, what we actually perform is to reference the object. therefore, if "assign an object to another object", the "Reference" is actually copied from one place to another. this means that if c = d is used for the object, both c and d point to the object that originally only d points. the following example will illustrate this point.
[Java]
<Span style = "font-size: 18px;"> package com. glacier. demo;
 
Class Tank {
Int level;
}
 
Public class Main {
 
/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
Tank t1 = new Tank ();
Tank t2 = new Tank ();
T1.level = 9;
T2.level = 47; www.2cto.com
System. out. println ("1: t1.level:" + t1.level + "t2.level :"
+ T2.level );
T1 = t2;
System. out. println ("2: t1.level:" + t1.level + "t2.level :"
+ T2.level );
T1.level = 27;
System. out. println ("3: t1.level:" + t1.level + "t2.level :"
+ T2.level );

}
 
} </Span>

Running result:
1: t1.level: 9 t2.level: 47
2: t1.level: 47 t2.level: 47
3: t1.level: 27 t2.level: 27
The tank class is very simple. Its two instances (t1 and t2) are created in main. assign a different value to the level field of each tank Class Object, assign t2 to t1, and then modify t1. in many programming languages, we may expect that t1 and t2 are always independent of each other. however, the value assignment operation references an object, so T2! This is because t1 and t2 contain the same references and point to the same object. (Originally, t1 contained a reference to an object, pointing to an object with a value of 9. when assigning values to t1, this reference is overwritten, that is, lost. The object that is no longer referenced will be automatically cleared by the garbage collector .)
This special imagination is often called "alias imagination", which is a basic method for java to operate objects. In this example, if you want to avoid alias problems, you can write as follows:
T1.level = t2.level;
In this way, the two objects are independent of each other, instead of binding t1 and t2 to the same object. however, you will soon realize that direct operations on the domain in the object can easily lead to confusion and violate the good object-oriented programming principles. this is not a small problem, so from now on, you should note that assigning values to objects may have unexpected consequences.

 

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.