Java's value is passed to the reference value

Value is passed: a recipient of the argument is the value to copy the original variables, how to change parameter values in the method will not affect the original variables.
Reference value: Method received parameter heap / stack address of the original variable in the process, if the value of the parameter is modified, the original value of the variable will be modified.

1. First, Java variables in two types: basic types, reference types.
That basic types: byte , Short , int , Long , a float , Double , char , Boolean . All basic types, are stored in the stack memory in.
Reference type is actually a class object , a reference to the type of the object in the memory heap memory in the reference address (pointer) of an object is stored in the stack memory in. (Array of objects, class objects).

2. When receiving method parameter, if the parameter type is a primitive type, then the copy of the original transmission is [] variable value change parameter value does not affect the value of the original variables. That value is passed. If the argument is an array or an object class, then the transmission is [reference], i.e., that the transfer object reference address in the stack. That is a reference value.

3. String objects once created will not be changed, any operations on String objects not affect the original object, any change operations will regenerate an object.

Posted about two code examples

public class A{
	public static void main(String[] args){
		int a = 123;	//值传递
		double d = 1.4286;	//值传递
		char[] chars = {'A','b','C','d','E'};	//值引用
		String str = "Hello world";	//值引用,但每一次改变String的值都只会创建一个新的String对象
		
		System.out.println("before="+a);
		System.out.println("before="+d);
		System.out.print("before=");
		for(int i =0;i<chars.length;i++){
			System.out.print(chars[i]+",");
		}
		System.out.println();
		System.out.println("before="+str);
		
		change(a,d,chars,str);
		
		System.out.println("after="+a);
		System.out.println("after="+d);
		System.out.print("after=");
		for(int i =0;i<chars.length;i++){
			System.out.print(chars[i]+",");
		}
		System.out.println();
		System.out.println("after="+str);
	}
	public static void change(int a,double d,char[] chars,String str){
		a=0;
		d = 3.1415;
		chars[1] = 'V';chars[3]='Z';
		str = "Fuck World";
	}	
}

class A{
	private Integer id;
	private String name;
	public void setId(Integer id){ this.id = id; }
	public Integer getId(){ return id; }
	public void setName(String name){ this.name = name; }
	public String getName(){ return name; }
	public String toString(){ return "a.id="+this.id+"a.name="+this.name; }
}
public class B {
    public static void main(String []args) {
		A a = new A();
		a.setId(12);	a.setName("Jack");
                System.out.println("Before: "+a.toString());
		change(a);
		System.out.println("After: "+a.toString());
    }
	public static void change(A a){// 值引用
		a.setId(10);	a.setName("Mary");
	}
}

 

 

 

 

 

Guess you like

Origin blog.csdn.net/J1014329058/article/details/89477813