The parameter passing of functions in java is described in detail

  • 2020-05-17 05:31:40
  • OfStack

Parameter passing of functions in java

Conclusion:

1. When an object (a reference to an object) is passed as a parameter, a reference (equivalent to a pointer) is passed. That is, changes made to the parameters within the function affect the original object.
2. When a base type or a wrapper set of a base type is passed as a parameter, a value is passed. That is, the changes made to the parameters within the function do not affect the original variables.
3. When an array (array reference) is passed as a parameter, a reference (equivalent to a pointer) is passed. That is, changes made to the parameters in the function will affect the original array.
4. When the String type (reference) is passed as a parameter, a reference is passed, except that a new String object will be generated when any changes are made to String, and the value of the original String object will not be changed. (but you can assign a reference to a new object to an old one, which gives the appearance that the original object has changed, not changed, but that the original reference to it points to the new object.)


package StringTest; 
 
class A{ 
  int a=1; 
  char b='A'; 
  public A(){} 
  public A(int _a,char _b){ 
    this.a=_a; 
    this.b=_b; 
  } 
  public String toString(){ 
    return "a="+this.a+",b="+this.b; 
  } 
} 
 
 
public class ReferenceTest { 
  public static A changeA(A classa){ 
    classa.a=2; 
    classa.b='B'; 
    return classa; 
  } 
   
  public static String changeString(String str){ 
     System.out.println(str.hashCode()); 
     str=str.toLowerCase(); 
     System.out.println(str.hashCode()); 
     return str; 
  } 
   
  public static int changeint(int a){ 
    a=a+1; 
    return a; 
  } 
   
  public static Integer changeInteger(Integer a){ 
    a=new Integer(9); 
    return a; 
  } 
   
  public static int[] changeintarray(int a[]){ 
    a[0]=10; 
    return a; 
  } 
   
  public static void printArray(int a[]){ 
    for(int i=0;i<a.length;i++){ 
      System.out.print(a[i]+" "); 
    } 
    System.out.println(); 
  } 
   
  public static void main(String[] args) { 
    // A custom object passes a reference  
    A a=new A(); 
    A b=changeA(a); 
    System.out.println(a); 
    System.out.println(b);  
    System.out.println("----------------------"); 
    //String The object is also passed as a reference as a parameter ( just String The value of the object cannot be changed , every 1 A modified String The value of the object is recreated 1 A new one String Object to hold the modified value. The original value does not change ) 
    String str1="HUHUALIANG"; 
    System.out.println(str1.hashCode()); 
    String str2=changeString(str1); 
    System.out.println(str2.hashCode()); 
    System.out.println(str1); 
    System.out.println(str2);  
    System.out.println("----------------------"); 
    // The basic type is value passing  
    int inta=8; 
    int intb=changeint(inta); 
    System.out.println(inta); 
    System.out.println(intb);  
    System.out.println("----------------------"); 
    // The wrapper set of the base type passes a value rather than a reference as a parameter  
    Integer c=new Integer(1); 
    Integer d=changeInteger(c); 
    System.out.println(c); 
    System.out.println(d);  
    System.out.println("----------------------"); 
    // An array passes a reference  
    int [] arraya={0,1,2,3}; 
    int [] arrayb=changeintarray(arraya); 
    printArray(arraya); 
    printArray(arrayb); 
  } 
} 

Operation results:

a=2,b=B
a=2,b=B
----------------------
711139030
711139030
226046678
226046678
HUHUALIANG
huhualiang
----------------------
8
9
----------------------
1
9
----------------------
10 1 2 3
10 1 2 3

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: