Understanding and analysis of pass through value calls in Java

  • 2020-04-01 03:37:15
  • OfStack

This article analyzed the pass-value calls in Java as an example. Share with you for your reference. Specific analysis is as follows:

Java operates on object instances by reference

What you can confirm is that the way objects are manipulated in Java is by reference. To understand this more deeply, I wrote the following code:

Start by defining a custom type

public class Person {  
     
    String name; 
     
    Person(String name){ 
        this.name = name; 
    } 
}

Here the name is public by default (not the same as the default attribute of class in C++)
Then the Main function is called 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 is as follows:
p1.name = Paul  p2.name = Griefen  
tmp.name = Paul p1.name = Griefen p2.name = Griefen

Yi? Why is this a result? As a CPPer I'm confused! That's a reference, so after you execute the following statement

Person tmp;  
tmp = p1; 
p1 = p2;

Because I understand this operation according to the concept quoted in C++, then TMP p1 and p2 should 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 references in Java are not the same as references in C++? !!!!!!!!! Is it the same as Pointers in C++?

Good! Let's follow the Pointers in C++ to understand this operation.

The first Person TMP. A pointer declaration is just like a pointer declaration in C++. In C++, it is clearly stipulated that the declaration of reference is not allowed to be written in this way alone, when the declaration of reference should point to an object, obviously the first step of reasoning is correct. That's a good start! And then TMP is equal to p1; P1 = p2; Obviously, TMP points to p1, and p1 points to p2. Check the output. It fits!
That means references in Java should be closer to Pointers in C++ than data manipulation in C++!

C++ pointer to achieve the above functions

Since the above Java manipulates objects in the same way as Pointers in C++, let's take a look at how the above way is implemented in C++

#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, the output is the same as the above Java run

The underlying implementation of any language is the same

Since the reference in Java looks the same as the C++ pointer why not just call it a pointer in Java? Obviously there is a difference. For example, a reference in Java cannot be ++, whereas a C++ pointer can and can be moved at will. Obviously at this point Java does a lot of work with its Pointers to limit it and make it more secure to run. But no matter how big the top layer looks, the bottom layer needs to apply for memory, memory to be used up after the release. This is a job to do in any language!

I hope this article has been helpful to your Java programming.


Related articles: