AliaSing problem in Java

xiaoxiao2021-03-06  15

The assignment action is based on the '=' operator. The meaning of assignment (assignment) is to obtain the value on the right side of the operator, copy the value to the left side of the operator. The right value can be an expression that recognizes constants, variables, or capable generating values, and the left value must be a clear, named variable.

The basic type of assignment is quite intuitive because the basic type is stored at the actual value, not the Object Reference. When you perform a basic type of assignment, a value is assigned to another value. For example, it will be copied to A. If you can then modify a value, B is certainly not affected if you will then modify a value. As a programmer, you will naturally be very natural in most cases.

But when you operate an object, what you do is actually its reference. So you "assign an object to another object", actually assign its reference from somewhere to the other. This means that if the two objects are written to C = D, they will cause c and d to point to the original D. The following examples use to illustrate this phenomenon.

Class Number {

INT I;

}

Public class associx {

Public static void main (String [] args) {

Number n1 = new number ();

Number n2 = new number ();

N1.i = 9;

N2.i = 47;

System.out.println ("1: n1.i" n1.i ", n2.i:" n2.i);

N1 = N2;

System.out.println ("2: N1.i" N1.I ", N2.I:" n2.i);

n1.i = 27;

System.out.println ("3: N1.I" N1.I ", N2.I:" n2.i);

}

}

Number Class is very simple. Its two entities (N1 and N2) are generated in main (). I of each Number entity is given a different value, and then N2 is assigned to N1, and then the content of N1 is changed. In many programming languages, you will expect N1 and N2 to be independent and uninterrupted, but because it is still refrene, the output you see is:

1: N1.I: 9, N2.I: 47

2: N1.I: 47, N2.I: 47

3: N1.I: 27, N2.I: 27

The specific process is such an object that creates a Number class in the Assignment class and assigns memory space. A reference called N1 points to it, and n1.i = 9; and a reference called N2 points to another new Objects of the Number class created, n2.i = 47. So the first printed is 1: N1.I: 9, N2.I: 47

Next, N1 = N2; this process is to assign N2 to N1, and you must pay attention. At this time, our operation is their Reference, and it is not to assign an object to another object. As a result, the N1 reference points to the object points to the N2 reference. Going deeply: Where is the object that n1 originally referenced? It was cleaned up as garbage. Back to the topic above, the result also caused N1 and N2 to point to an object, so result output to 2: N1.I: 47, N2.I: 47;

In the third step, n1.i = 27 also reaches the I value of the I value in the object pointing at N1 and N2 to 27, then the result is natural output 3: N1.I: 27, N2.I: 27

Also in the call function, an aliasing problem occurs.

Class letter {

Char C;

}

Public class passObject {

Static void f (letter y) {

Y.c = 'z';

}

Public static void main (String [] args) {

Leeter x = new letter ();

X.c = 'a';

System.out.println ("1: x.c:" x.c);

f (x);

System.out.println ("2: x.c:" x.c);

}

}

In many programming languages, f () will create a copy of its quoter letter y within the function range. But because it is actually a Reference, this line:

Y.c = 'z';

It is actually faster to the original object outside the F (). The result results show this:

1: x.c: a

2: x.c: z

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

New Post(0)