Understanding _java in Java and C + + for transfer of value, reference passing, and pointer mode

Source: Internet
Author: User
Tags call by reference stringbuffer

Java values pass the understanding:

Code 1:

public class Test {   
   
  /**   
   * @param args   
   *  
  /public static void main (string[] args) {     
    StringBuffer buffer = new StringBuffer ("Colin");   
    Schange (buffer);   
    SYSTEM.OUT.PRINTLN (buffer);   
      
  public static void Schange (StringBuffer str) {   
    str= new StringBuffer ("Huang");   
  }   
   

Code 2:

public class Test {   
   
  /**   
   * @param args   
   *  
  /public static void main (string[] args) {     
    StringBuffer b uffer= New StringBuffer ("Colin");   
    Schange (buffer);   
    SYSTEM.OUT.PRINTLN (buffer);   
      
  public static void Schange (StringBuffer str) {   
    //str= new StringBuffer ("Huang");       Str.append ("Huang");   
  }   
   

Two more graphs to explain the above code 1, code 2:

Original state

Code 1 Diagram:

Code 2 Understanding:

In code one, the copy of the reference points to a new object. But the original object is still unchanged.

In code two, the copy of that reference changes the original object.

This is the value delivery of java.

The difference between two types of delivery in C + +:

For C + + value delivery, reference passing, pointer mode with the following code to understand, run the test yourself

#include <stdio.h> 
#include <iostream> 
#include <typeinfo> 
void byvalue (int a) 
{ 
  A = a + 1; 
} 
 void ByRef (int& a) 
{ 
  A = a + 1; 
} 
 void Bypointer (int* a) 
{ 
  *a = *a + 1; 
} 
 int main (int argv, char** args) 
{ 
  int v = 1; 
  Byvalue (v); 
  ByRef (v); 
 
  Pass by Reference   Bypointer (&v); 
 
  Pass by Value   int* VP = &v; 
  Bypointer (VP); 
   
  Std::cout << v << std::endl; Std::cout << typeid (VP). Name () << Std::endl;  
Std::cout << typeid (&VP). Name () << Std::endl;    Std::cout << "End" << Std::endl; 
}

The first is value-passing, and the second function is a reference pass, but the next two, the same function, one call is called by reference, and the other is calls by value.

Because:

Bypointer (VP); There is no change in the VP. Value Delivery

Bypointer (&V); Changed the V. Reference delivery (you might say that this is actually the address of V, and Bypointer cannot change the address of V, so this is called by value.) This may sound justified, but the address of V, which is pure data, does not exist in the caller's code, and for the caller, only V, and V is indeed changed by the Bypointer function, the result of which is the behavior of call by reference. From behavior consideration, is the meaning of the evaluation strategy. If everything is abstracted into values, from the data to consider the problem, there is no need to introduce the concept of evaluation strategy to confuse. )

Nob: The above understanding recognizes, the supplementary pointer way may use two kinds of ways, the value passes: passes a pointer, the reference passes: passes a variable the address or the reference, if uses typeID (x). Name () to view &v and VP Discovery are point types, so two manifestations, Same kind of result. You might think of me like this.

Byvalue (&v);  Error

, while passing different types of parameters in C + + directly compiles without passing.


Summarize:

So I think it's just a matter of passing the value and quoting the memory.

Share memory is a reference, copy memory is the value (put aside some special cases)

Such words:

C + +: Default pass value, reference pass reference, the pointer to understand the individual (the pointer can be understood to be both to pass the value, but also to pass the reference, and produce the same result)

JAVA: The underlying data type value is passed, and the object is also a value pass (copy a copy of the object's reference)

C #: Value type pass value, reference type pass reference, Ref/out special understanding

Java and C # 's string to be specially understood, the representation is the value, actually depends on the virtual machine realization


Related Article

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.