This article analyzes the call by value in Java with examples. Share it with everyone for your reference. The specific analysis is as follows:
Java operates object instances by reference
What can be confirmed is that the way to operate objects in Java is to operate objects by reference. In order to understand this more deeply I wrote the following code:
First define a custom type. Copy the code. The code is as follows: public class Person {
String name;
Person(String name){
this.name = name;
}
}
The name here defaults to public (which is different from the default attribute of class in C++)
Then the call in the Main function is as follows:
Copy the code as follows: public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Person p1 = new Person("Paul");
Person p2 = new Person("Griefen");
System.out.println("p1.name = " + p1.name + " p2.name = " + p2.name);
Person tmp;
tmp = p1;
p1 = p2;
System.out.println("tmp.name = " + tmp.name + " p1.name = " + p1.name + " p2.name = " + p2.name);
}
}
The output at this time is as follows:
Copy the code as follows: p1.name = Paul p2.name = Griefen
tmp.name = Paul p1.name = Griefen p2.name = Griefen
Huh? Why is this a result? As a CPPer I am confused! It is a reference, so after executing the following statement, copy the code as follows: Person tmp;
tmp = p1;
p1 = p2;
Because I understand this operation according to the concept of reference in C++, then tmp p1 p2 should all be the same object at this time, that is, they should all point to the object p2. But the output clearly proves that this understanding is wrong! So since references in Java are not the same thing as references in C++? ! ! ! Is it the same thing as a pointer in C++?
good! Let's understand this operation process according to pointers in C++.
First of all, Person tmp; declares a pointer, which is similar to the pointer declaration in C++. In C++, it is clearly stipulated that the reference declaration cannot be written separately in this way. When the reference declaration is made, it should point to an object. Obviously, the reasoning in the first step is correct. This is a good start! Then tmp = p1; p1 = p2; Obviously tmp points to p1 and p1 points to p2. Check the output. It was found to be consistent!
That means that if references in Java are compared with data operations in C++, they should be more similar to the concept of pointers in C++!
Pointers in C++ implement the above functions
Since the above method of operating objects in Java is similar to the pointer in C++, let's take a look at how to implement the above method in C++. Copy the code as follows: #include "stdafx.h"
#include <string>
#include <iostream>
class Person
{
public:
std::string name;
public:
Person(std::string name)
{
this->name = name;
};
};
int _tmain(int argc, _TCHAR* argv[])
{
Person* p1 = new Person("Paul");
Person* p2 = new Person("Griefen");
std::cout<< "p1.name " << p1->name << " p2.name = " << p2->name << std::endl;
Person* tmp;
tmp = p1;
p1 = p2;
std::cout<<"tmp.name" << tmp->name << " p1.name " << p1->name << " p2.name = " << p2->name << std::endl ;
delete tmp;
delete p1;
return 0;
}
After debugging, I found that the output result is the same as the above Java running result.
The underlying implementation of any language is the same
Since the reference effect in Java seems to be the same as the pointer in C++, why isn't it called a pointer directly in Java? Obviously there are differences. For example, references in Java cannot perform ++ operations, but pointers in C++ can and can be moved at will. Obviously at this point Java has done a lot of work on its pointers to restrict them and make them run safer. But no matter how big the upper layer seems to be, when you get to the bottom layer, you need to apply for memory, and the memory must be released after it is used up. This is work in any language!
I hope this article will be helpful to everyone’s Java programming.