java 引用的传递
案例:
package csdn.zyl.demo;
class Ref1{
int temp = 10;
}
public class RefDemo {
class Ref1{
int temp = 10;
}
public class RefDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Ref1 ref1 = new Ref1();
ref1.temp = 20;
System.out.println(ref1.temp);
tell(ref1);
System.out.println(ref1.temp);
}
//引用的传递
public static void tell(Ref1 r2)
{
r2.temp=30;
}
// TODO Auto-generated method stub
Ref1 ref1 = new Ref1();
ref1.temp = 20;
System.out.println(ref1.temp);
tell(ref1);
System.out.println(ref1.temp);
}
//引用的传递
public static void tell(Ref1 r2)
{
r2.temp=30;
}
}
结果
20
30
30
图解:
案例二 :
package csdn.zyl.demo;
class Ref1{
int temp = 10;
}
public class RefDemo {
class Ref1{
int temp = 10;
}
public class RefDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
String temp = "Hello";
System.out.println(temp);
tell(temp);
System.out.println(temp);
}
public static void tell(String str2)
{
str2 = "jike";
}
// TODO Auto-generated method stub
String temp = "Hello";
System.out.println(temp);
tell(temp);
System.out.println(temp);
}
public static void tell(String str2)
{
str2 = "jike";
}
}
结果:
Hello
Hello
Hello
图解: