标签:参考 href java语言 sys detail ble tail 接收 vat
@Author: TianchiLiu
引用传递:
也称为传地址。方法调用时,实际参数的引用(地址,而不是参数的值)被传递给方 法中相对应的形式参数,函数接收的是原始值的内存地址。在方法执行中,形参和实参内容相同,指向同一块内存地址,方法执行中对引用的操作将会影响到实际对象。
对象和数组是引用传递
public class Test1 {
static class a{
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int i = 1;
System.out.println("基本类型数据传入方法前:"+i);//基本类型数据传入方法前:1
change(i);
System.out.println("基本类型数据传入方法后:"+i);//基本类型数据传入方法后:1
a a1 = new a();
System.out.println("对象类型数据传入方法前:"+a1);//对象类型数据传入方法前:对象等号.Test1$a@2a139a55
changea(a1);
System.out.println("对象类型数据传入方法后:"+a1);//对象类型数据传入方法后:对象等号.Test1$a@2a139a55
int shuzu[] = new int[2];
System.out.println("数组传入方法前:"+shuzu);//数组传入方法前:[I@6d06d69c
changeshuzu(shuzu);
System.out.println("数组传入方法后:"+shuzu);//数组传入方法后:[I@6d06d69c
}
private static void changeshuzu(int[] shuzu) {
// TODO Auto-generated method stub
shuzu = new int[3];
System.out.println("数组传入方法中:"+shuzu);
}
private static void changea(a a1) {
// TODO Auto-generated method stub
a1 = new a();
System.out.println("对象类型数据传入方法中:"+a1);//对象类型数据传入方法中:对象等号.Test1$a@15db9742
}
private static void change(int i) {
// TODO Auto-generated method stub
i = 100;
System.out.println("基本类型数据传入方法中:"+i);//基本类型数据传入方法中:100
}
}
/*
* 传入方法之前i:15
传入方法之后i:20
传入方法之前数组内容:1,2,3
传入方法之后数组内容:10,20,30*/
public class Test2 {
static class b{
int i = 15;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
b b1 = new b();
System.out.println("传入方法之前i:"+b1.i);
changeb(b1);
System.out.println("传入方法之后i:"+b1.i);
int shuzu[] = {1,2,3};
System.out.println("传入方法之前数组内容:"+shuzu[0]+","+shuzu[1]+","+shuzu[2]);
changeshuzu(shuzu);
System.out.println("传入方法之后数组内容:"+shuzu[0]+","+shuzu[1]+","+shuzu[2]);
}
private static void changeshuzu(int[] shuzu) {
// TODO Auto-generated method stub
shuzu[0] = 10;
shuzu[1] = 20;
shuzu[2] = 30;
}
private static void changeb(b b1) {
// TODO Auto-generated method stub
b1.i = 20;
}
}
/*
public class Test3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String a = "a1";
System.out.println("传入方法前:"+a);//传入方法前:a1
changestring(a);
System.out.println("传入方法后:"+a);//传入方法后:a1
Integer a1 = 1;
System.out.println("传入方法前:"+a1);//传入方法前:1
changeInteger(a1);
System.out.println("传入方法后:"+a1);//传入方法后:1
}
private static void changeInteger(Integer a1) {
// TODO Auto-generated method stub
a1 =100;
}
private static void changestring(String a) {
// TODO Auto-generated method stub
a = "a2";
}
}
参考:obession
标签:参考 href java语言 sys detail ble tail 接收 vat
原文地址:https://www.cnblogs.com/TianchiLiu/p/9856244.html