以对象为参数,传递对象的引用首地址 class ValHold{ public int i = 10; } public class ObParm{ public static void main(String argv[]){ ObParm o = new ObParm(); o.amethod(); } public void amethod(){ ValHold v = new ValHold(); v.i=10; System.out.println("Before another = "+ v.i); another(v); System.out.println("After another = "+ v.i); }//End of amethod public void another(ValHold v){ v.i = 20; System.out.println("In another = "+ v.i); }//End of another }输出结果: Before another = 10 In another = 20 After another = 20
以变量为参数,传递值public class Parm{ public static void main(String argv[]){ Parm p = new Parm(); p.amethod(); }//End of main public void amethod(){ int i=10; System.out.println("Before another i= " +i); another(i); System.out.println("After another i= " + i); }//End of amethod public void another(int i){ i+=10; System.out.println("In another i= " + i); }//End of another } 输出结果: Before another i= 10 In another i= 20 After another i= 10
class ValHold{
public int i = 10;
}
public class ObParm{
public static void main(String argv[]){
ObParm o = new ObParm();
o.amethod();
}
public void amethod(){
ValHold v = new ValHold();
v.i=10;
System.out.println("Before another = "+ v.i);
another(v);
System.out.println("After another = "+ v.i);
}//End of amethod
public void another(ValHold v){
v.i = 20;
System.out.println("In another = "+ v.i);
}//End of another
}输出结果:
Before another = 10
In another = 20
After another = 20
public static void main(String argv[]){
Parm p = new Parm();
p.amethod();
}//End of main
public void amethod(){
int i=10;
System.out.println("Before another i= " +i);
another(i);
System.out.println("After another i= " + i);
}//End of amethod
public void another(int i){
i+=10;
System.out.println("In another i= " + i);
}//End of another
}
输出结果:
Before another i= 10
In another i= 20
After another i= 10