Object-oriented and scope knowledge points

The problems involved in passing a reference type variable as a parameter in Java :
address passing problem (different from value passing)

class test{
    
    
		public int a = 10;
}

public class TestValObject {
    
    
	public static void another(test v1,int i) {
    
    
		i=0;
		v1.a=40; // 局部变量v1存储 test变量的地址,(10)->(40)
		test v0 = new test();
		v1 = v0; // v1 指向改变
		System.out.println(v1.a);
		System.out.println(i);
	}
	public static void main(String[] args) {
    
    
		test v1 = new test();
		int i=20;
		another(v1, i); // 传递地址
		System.out.println(v1.a);
		System.out.println(i);
	}
}

// 10 0 40 20

(Add one silently, I was so miserable!)

Guess you like

Origin blog.csdn.net/qq_43341057/article/details/104851882