Two common sense that cainiao must understand-value transfer or reference Transfer

Source: Internet
Author: User

Even though this article is the foundation, many people may not be clear about it. "Two common sense"

I. object and reference)

There are a lot of books that have been vague about the relationship between objects and references, and even some are simply incorrect statements. We must address this issue.
Have a clear understanding.
We know:
A A = new ();
Generates an object of type A. A is a reference of this object, that is, a points to the real object in heap, while a and other basic data types
Stored in the stack together. That is, the object is controlled by reference. At the underlying layer, A is more like a pointer.
For some books, A is an object, and it is no big problem for beginners, because operations on a are operations on objects pointed to by.
The problem is that when the direction of a changes, the object expression of a cannot adapt to the needs of program design.
Let's look at a simple program:

Class
{
Private int I = 0;
Public void SETI (int x)
{
I = X;
}
Public int Geti (){
Return I;
}
}

Public class myref1 {

Public static void main (string [] ARGs ){
A A = new ();
A B = new ();
A. SETI (10 );
B. SETI (15 );
System. out. println ("a's I =" + a. getI ());
System. out. println ("B I =" + B. getI ());
A = B;
A. setI (20 );
System. out. println ("a's I =" + a. getI ());
System. out. println ("B I =" + B. getI ());

}

}
I think the output of the program should be considered:
I = 10 of
I = 15 of B
I = 20 of
I = 15 of B
First, the second row should have no difference. The third row is the I value after setting a. The problem is that the fourth row will not output I = 15. The correct result is:
I = 20
Therefore, a and B are all references to objects. When we assign a to B, a has re-pointed to B and pointed to the operation of a after a change,
Is the operation on B.
Of course, those who stick to the saying "a, B is the object" can still explain this question: Yes, object a "is changed to" object B ". There is nothing.
Normal.
Let's take a look:

We know that java defines constants through final:
Final int I = 10;
When we re-assign a value to a constant, a compilation error occurs:
I = 5; // compilation failed
We can also use final to define constant objects:
Final A a = new ();
In this case, we cannot assign a value to.

If a is an object, the object cannot be changed. In fact, a is only a reference and can only point to the object originally pointed,
It does not mean that the State of the object it refers to cannot be changed. Therefore, we can modify the state of the object without changing the original orientation of.
Change, check the program:

Class
{
Private int I = 0;
Public void setI (int x)
{
I = x;
}
Public int getI (){
Return I;
}
}

Public class MyRef1 {

Public static void main (String [] args ){

Final A a = new ();
System. out. println (a. getI ());
A. setI (8 );
System. out. println (a. getI ());

}

}

If a is an object, it is impossible.
A. setI (8 );
Actually, a is a reference, and the program can be compiled and run:
Display: 8

In short,Java uses renfence to manipulate objects,Is the basis for deep learning java knowledge, for example:

2. Whether the java parameter is passed by value or reference

Let's look at the program first:
Public class MyRef2 {

Static int x = 10;
Static int y = 20;
Public static void fangfa (int I)
{
I ++;
X = I;
}

Public static void main (String [] args ){
System. out. println ("x =" + x );
System. out. println ("y =" + y );
MyRef2.fangfa (y );
System. out. println ("x =" + x );
System. out. println ("y =" + y );

}

}
Obviously:
X = 10
Y = 20
X = 21
Y = 20
The value of y has not changed. MyRef2.fangfa (y) only uses the value of y, and the I ++ in it does not affect y itself.
Obviously, the java parameter is a value transfer, but why is there a reference transfer statement?
Take a look at the following program:
Class
{
Private int I = 0;
Public void setI (int x)
{
I = x;
}
Public int Geti (){
Return I;
}
}

Public class myref1 {

Public static void seta1 (A newa, int T)
{
Newa. SETI (t );

}

Public static void main (string [] ARGs ){
A A = new ();
System. Out. println (A. Geti ());
Myref1.seta1 (A, 30 );
System. Out. println (A. Geti ());
}

}
According to the value transfer statement, myref1.seta1 (A, 30); uses a replica of the object referred to by a, and ultimately does not work for this object
The fact is that the method plays a role on this object, and the program will display 0, 30. So, is the Java parameter a value transfer error?
Dad told you: No!
We should remember that A is only the object's reference, and the reference's replica points to the same object as the original reference.
The operations we perform on the replica are the same as those on a, and finally the operations to point to the object. Therefore,Java parameters. Only values are passed.

Author: laodie
QQ: 16643190
Tel: 110 to 120 to 119 to 168099999 press 3
 

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.