java-- reference value and pass transfer

Value transfer

When the method is called, the operation argument by any parameter copy its contents incoming internal method, then the contents of the received parameter is a copy of the argument value, so the parameter in the process, are simply An operation of this copy does not affect the content of the original value.

 

First look at an example:

static void valueCross public (int age, a float weight) { 
	System.out.println ( "incoming age value:" + age); 
	System.out.println ( "incoming weight values:" + weight); 
	age = 23 is; 
	weight = 60; 
	System.out.println ( "modified age value:" + age); 
	System.out.println ( "modified weight values:" + weight); 
} 
	
	
public static void main (String [ ] args) { 
	int Age = 10; 
	int weight = 50; 
	valueCross (Age, weight); 
	System.out.println ( "after the method executes Age:" Age +); 
	( "methods of performing System.out.println weight: "+ weight); 
		
}

  

operation result:

Incoming age: 10 
passed weight value: 50.0 
age modified value: 23 is 
weight modified value: 60.0 
10: the method is performed after age 
weight of method execution: 50

  

We can see that after valueCross method of execution, the value of age and weight of the argument has not changed, which is why?

First, the program is running, start the main method begins execution, then the JVM main () method to press the stack into the virtual machine a stack frame, is the current stack frame, used to store the main () in the local variable table (including parameters), operand stack, and other information for export, and w are such a mian () method of local variables, it can be concluded that, age and weight stack frames are located lying in the method mian

ValueCross then call a method, the JVM case valueCross () method to press the virtual machine into a stack frame, is the current stack frame information for local variables stored valueCross method; age and weight is thus a method where lying valueCrossTest the stack frame, and their values ​​are obtained copy of a copy, as shown from the values ​​of a and w:

So both age and weight corresponding to the content is not the same, changes in valueCross method only the content they stack, and did not modify the content of the stack main method

 

Passed by reference

"Reference" is an address value pointing to the real content of the method invocation, the argument of the address by a method call is passed to the corresponding parameter in vivo, formal and actual parameters point to the same memory address of the parameter the real content of the operation will affect.

 

 First, a Person class code as follows:

public class Person {
	
	private String name;
	private int age;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	

}

  

 Test Class Code:

{class TestArr public 
	
	
	public static void PersonCross (the Person person) { 
		 System.out.println ( "incoming person's name:" + person.getName ()); 
		 person.setName ( "Ao prop"); 
		 the System.out. ( "name after the re-assignment method:" + person.getName ()) the println; 
	} 
	
	public static void main (String [] args) { 
		the Person new new P = the Person (); 
		p.setName ( "us"); 
		p.setAge (. 4); 
		PersonCross (P); 
		System.out.println ( "the method of performing name:" + p.getName ()); 
	} 
	
}

  

1 Test results:

Incoming person's name: Rebels 
name after the re-assignment within the method: Ao propylene 
after the execution method name: Ao propionate

  

We can see PersonCross method of execution, name value of the person is changed

 

Look at the following piece of code:

{class TestArr public 
	public static void PersonCross (the Person person) { 
		 System.out.println ( "incoming person's name:" + person.getName ()); 
		 person new new = the Person (); // newly added code 
		 person .setName ( "Ao prop"); 
		 System.out.println ( "after re-assignment the method name:" + person.getName ()); 
	} 
	
	public static void main (String [] args) { 
		the Person the Person new new = P (); 
		p.setName ( "Zha"); 
		p.setAge (. 4); 
		PersonCross (P); 
		System.out.println ( "the method of performing name:" + p.getName ()); 
	} 
	
}

  

Test Results:

Incoming person's name: Rebels 
name after the re-assignment within the method: Ao propylene 
name after the execution method: Rebels

  

There did not find anything different, name of the person the value of the PersonCross execution method has not changed, which is why?

We know, java objects and arrays are stored in the heap, and heap memory is shared by the threads, so that when the main method of execution, will open up a memory in the heap memory is used to store all content p object, then and then create a reference stack memory storage real address p p objects in the heap area, as shown below:

When executing to PersonCross method, because there is such a line of code in the method: person = new new the Person (), this time in the JVM heap memory space has opened up a memory, assuming that address xo2222, so now the person it points to the xo2222 memory block, modify person's name value is a value xo2222 now modify this memory space will not change the value of xo3333, so the test result has not changed name 2  

Guess you like

Origin www.cnblogs.com/wgblog-code/p/11297638.html