public class Test1 { public static void changeValue(int value){ value = 0; } public static void main(String[] args) { int value = 2010; changeValue(value);//根据”变量的就近原则”,只是changeValue里面的变量value为0,与mian方法中的变量无关 System.out.println(value); } } 结果为=2010 |
---|
public class Value{ public int i=15; } public class Test05 { public static void main(String[] args) { Test05 t05=new Test05(); t05.first(); } public void first(){ int i=5; Value v=new Value(); second(v,i); v.i=25; } public void second(Value v,int i){ i=0; v.i=20; Value val=new Value(); v=val;//将val的变量地址传给v System.out.println(v.i+” “+i); } } //Test05调用first调用second 结果为=``15 0 |
---|
public class People { int eyes = 2; } public class JustDoIt0801 { public static void main(String[] args) { // 基本类型 int i = 9; i); System.out.println(i); // 引用类型 People p = new People(); p); System.out.println(p.eyes); } private static void changeInt(int x){ x = x + 9; } private static void changePeople(People x){ x.eyes = x.eyes + 1; } } 结果为=9 3 |
---|
public class Example{ static int i=1; static{ i=i+1; } public static void main(String[] args){ i,5); i); } static void increment(int n,int m){ n=n+m; } static void display(int n){ System.out.println(n); } static{ i=i+1; } } //一个类中可以有多个静态块,按先后顺序执行 结果为=3 |
---|
public class TempTest { private void test1(int a){ a = 5; System.out.println(“test1方法中的a=”+a);//5 } public static void main(String[] args) { TempTest t = new TempTest(); int a = 3; t.test1(a);//传递后,test1方法对变量值的改变不影响这里的a System.out.println(“main方法中的a=”+a);//3 } } 结果为? |
---|
public class TempTest1 { private void test1(A a){ a.age = 20; //a.age = 20; System.out.print(“test1方法中的age=”+a.age); a.age=10 } public static void main(String[] args) { TempTest1 t = new TempTest1(); A a = new A(); a.age = 10; t.test1(a); System.out.print(“main方法中的age=”+a.age); a.age=10 } } class A{ public int age = 0; } 结果为20 20 |
---|