"Java programming ideas" Book: assignment operator

Assignment operators are frequently found in many programming languages, before I understood it all " copies the value of the right to the left ," and, of course, can not be left as a constant, for example, can say a = 4 can not say 4 = A . Only recently read "Java programming ideas," the operator of this chapter found that in some cases "=" the use of results and we expect a far cry (if it's the same as before or with understanding simple words).

One of the features of the reason for this deviation is the object-oriented programming: Java in the operation of an object is often a reference to the operation of the object, so before understanding " will assign one object to another object " actually becomes " the reference is copied from one place to another place " , such as a = b , then a and b of these two references to the original object becomes b pointed-to object. The basic type of operation, then sucked b copy the contents of A , and b of the content will not be affected. A simple example is as follows:

package test;

class Test{
	int i;
	public Test(int i) {
		super();
		this.i = i;
	}
	
}
public class Blog {
	public static void main(String[] args) {
		Test t1 = new Test(5);
		Test t2 = new Test(11);
		System.out.println("t1:"+t1.i+" t2:"+t2.i);
		t1 = t2;
		System.out.println("t1:"+t1.i+" t2:"+t2.i);
		t1.i = 20;
		System.out.println("t1:"+t1.i+" t2:"+t2.i);
	}

}

Results are as follows:

t1: 5 t2: 11
           t1 11 t2 11
           t1 20 t2: 20

We found that when executed t1 = t2 after this operation, modification t1 will also change t2! This is so because there is the assignment operator t1 and t2 refer to two point to the same object. One might wonder: even point to the same object reference or two separate ah, why change a point to put the two has changed? The explanation given "Java programming ideas" are: the original t1 contains points 5 cited in the pair t1 covered when assigned, that is lost; and there will be an object that no longer referenced "garbage collector" automatic cleanup .

This phenomenon is called "Alias phenomenon" , it is the way Java has been the basic operation of the object. To avoid this problem, you can use direct mode operation fields:

t1.i = t2.i;

So that it can remain independent object, but "Java programming ideas" is also mentioned: direct manipulation of objects in the domain easily lead to confusion, and contrary to the principles of good object-oriented programming. This point I do not have a more specific understanding.

Further, an object will pass to create alias problems Method:

package test;

class Test{
	int i;
	public Test(int i) {
		super();
		this.i = i;
	}
	
}
public class Blog {
	public static void main(String[] args) {
		Test t1 = new Test(5);
		System.out.println("t1:"+t1.i);
		f(t1);
		System.out.println("t1:"+t1.i);

	}
	public static void f(Test y){
		y.i = 7;
	}
}

The method appears to f only produces its scope in Test an object class, but the parameters are passed by reference in fact, the method f actually affected f () other than the object.

In short, this phenomenon is considered a trap programming noteworthy.

发布了5 篇原创文章 · 获赞 0 · 访问量 149

Guess you like

Origin blog.csdn.net/dollyandkalin/article/details/82665028