实践是检验真理的唯一标准,java代码:
public class TestReference {
public static void main(String[] args) {
String b = "hi";
String a = "hello";
a = b;
b = "love";
System.out.println("a: " + a);
// String类型与其他类型不同,它初始化后不会改变,只能重新生成一个新的String变量
// ==========================================================
Test test = new Test();
test.setId(1);
Test test2 = new Test();
test2.setId(2);
System.out.println("before swap");
System.out.println(test);
System.out.println(test2);
swap(test, test2);
// Test test3 = null;
// test3 = test;
// test = test2;
// test2 = test3;
System.out.println("after swap");
System.out.println(test);
System.out.println(test2);
}
public static void swap(Test a , Test b) {
Test temp;
temp = a;
a = b;
b = temp;
}
}
class Test{
private int id;
public void setId(int id) {
this.id = id;
}
public int getId(){
return id;
}
// public String toString(){
// return id + "";
// }
}
输出结果:
a: hi
before swap
Test@c17164
Test@1fb8ee3
after swap
Test@c17164
Test@1fb8ee3
得出结论:
1、String类型与其他类型不同,它初始化后不会改变,只能重新生成一个新的String变量。b = "love";等价于 b= new String(“love”);
2、对于数组中的元素交换,可以使用下文中的public static void swap(int[] data, int a, int b)方法;
对于非数组中的元素交换,可以使用最原始的交换方法,可以实现交换;也可以使用下文中包装类的方式、外部内联的方式,视情况而定
下面是引用高玩的帖子
=================================================================================================================================
- void swap(int&a ,int&b)
- {
- int temp;
- temp = a;
- a = b;
- b = temp;
- }
但在JAVA中用这种方法是行不通的,因为“Java对普通类型的变量是不支持引用传递的”。
怎么办呢?
1. 可以像下面这样通过传数组(也属于传值)的方法来完成对换(在很多排序算法里面就是这么干的):
- public static void swap(int[] data, int a, int b) {
- int t = data[a];
- data[a] = data[b];
- data[b] = t;
- }
2. 也可以通过重新定义个类(在JAVA中我们可以通过使用int的包装类---Integer,然后将其作为值的引用传到函数中,但这个Integer包装类也不允许你来改变它的数据域;但这不防碍我们用自己的包装类,比如说下面实现的MyInteger):
- //MyInteger: 与Integer有些类似,但是其对象可以变值
- class MyInteger {
- private int x; // 将x作为唯一的数据成员
- public MyInteger(int xIn) { x = xIn; } // 构造器
- public int getValue() { return x; } // 得到值
- public void insertValue(int xIn) { x = xIn;} // 改变值
- }
- public class Swapping {
- // swap: 传对象引用
- static void swap(MyInteger rWrap, MyInteger sWrap) {
- // 变值过程
- int t = rWrap.getValue();
- rWrap.insertValue(sWrap.getValue());
- sWrap.insertValue(t);
- }
- public static void main(String[] args) {
- int a = 23, b = 47;
- System.out.println("Before. a:" + a + ", b: " + b);
- MyInteger aWrap = new MyInteger(a);
- MyInteger bWrap = new MyInteger(b);
- swap(aWrap, bWrap);
- a = aWrap.getValue();
- b = bWrap.getValue();
- System.out.println("After. a:" + a + ", b: " + b);
- }
- }
3. 由于java 中的参数传递都是采用的值传递方式,这不防碍我们用swap的时候采用外部内联的方式:
- public class Swap2 {
- public static void main(String args[]){
- Swap2 sw = new Swap2(1,2);
- System.out.println("i is" + sw.i);
- System.out.println("j is" + sw.j);
- sw.swap();
- System.out.println("i is" + sw.i);
- System.out.println("j is" + sw.j);
- }
- int i,j;
- public Swap2(int i, int j){
- this.i = i;
- this.j = j;
- }
- public void swap(){
- int temp;
- temp = i;
- i = j;
- j = temp;
- }
- }
- public class Swap1 {
- public static void Swap1(Integer a, Integer b){
- Integer temp = a;
- a = b;
- b = temp;
- }
- public static void main(String args[]){
- Integer a,b;
- a = new Integer(10);
- b = new Integer(20);
- Swap1.Swap1(a, b);
- System.out.println("a is " + a);
- System.out.println("b is " + b);
- }
- }
======================================================================================================
Swap1 结果:
a is 10
b is 20
Swap2 结果:
i is1
j is2
i is2
j is1
普及知识:
Java是传值还是传引用:http://www.bccn.net/Article/kfyy/java/jszl/200601/3069.html
在C/C++/JAVA中实现swap:http://www.cs.utsa.edu/~wagner/CS2213/swap/swap.html
参考:
http://wzdoxu.iteye.com/blog/251988
http://hi.baidu.com/aleczhou/blog/item/975ad262391313d9e7113aa0.html
原文:http://blog.youkuaiyun.com/dadoneo/article/details/6577976