刚又看了个帖子,关于java参数传递的。我不认为我把这个问题彻底搞明白了,但我坚信真正搞明白这个问题的同学应该是屈指可数的。
先看几个问题吧,希望明白的和不明白的同学都回复下,大家共同把这个问题搞明白。
1
int a = 1;
int b = a;
b = 2;
System.out.println("a=" + a);
System.out.println("b=" + b);
如果有人觉得这个不像是参数传递那么我们写成下面的形式:
public static void change(int b) {
b = 2;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int a = 1;
change(a);
System.out.println("a=" + a);
}
2 和1差不多,我们换成String 来看看,String可不是基本类型哦。
public static void main(String[] args) {
// TODO Auto-generated method stub
String s1 = "abc";
String s2 = s1;
s2 += "d";
System.out.println("s1=" + s1);
System.out.println("s2=" + s2);
}
如果认为这不叫参数传递可以像1那样修改下。
public static void change(String s2) {
s2 += "d";
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String s1 = "abc";
change(s1);
System.out.println("s1=" + s1);
}
3 最后看下引用类型或是说非基本类型再或者是对象类型,以int数组来举例。
int[] a = new int[1];