在java中交换对象

我已经研究过java是通过引用传递但是当我执行下面的代码时,字符串在主方法中没有交换为什么?

static void swap(String s1, String s2){ String temp = s1; s1=s2; s2=temp; } public static void main(String[] args) { String s1 = "Hello", s2 = "world"; swap(s1, s2); System.out.println(s1 + s2); } 

你研究了错误的来源。 Java是按值传递的。 这是您可以学习的更多来源。 从这里你可以找到与你的相似的例子的讨论。

1.首先, Javapass by Value (即通过复制 )。

2.您正在 reference Not the Object传递给方法。

例如:

 public class Test{ private String x; public void go(String s){ this.x = s; } public static void main(String[] args){ Test t = new Test(); String a = new String("Bye"); t.go(a); } } 

上面的代码不是为了展示如何进行交换,而是为了使重要的一点可见。

  • 通过将类型为String Object reference variable 的参数“a”传递 给参数“s”(也是 String类型 Object reference variable go(String s)调用 go(String s)方法时, 它只是传递的引用价值/复制。

  • reference 所做的任何更改都 将影响堆上的String对象,而不是 the reference

传递给方法的是对象引用的副本 。 因此,无论您重新分配引用多少次,原始引用都不会受到影响。

尝试这个:

 static void reverse(StringBuilder builder) { builder.reverse(); } public static void main(String[] args) { StringBuilder builder = new StringBuilder("hello"); System.out.println(builder); reverse(builder); System.out.println(builder); } 

但是,下面的方法不会对传入的原始对象产生任何影响。

 static void swap(StringBuilder builder) { builder = null; } 

Java总是按值传递引用。 阅读此答案以获取更多详细信息 但是,您可以通过数组或包装类交换两个字符串。


编辑:

@dasblinkenlight显示了如何使用数组交换两个字符串,以下是使用包装器类的示例:

 public class WrapperString { public String text; public WrapperString(String text){this.text = text} @Override public String toString(){return text;} } 

 public static void swap(WrapperString a, WrapperString b) { String temp = a.text; a.text = b.text; b.text = temp; } public static void main(String[] args) { WrapperString s1 = new WrapperString("Hello"); WrapperString s2 = new WrapperString("World"); swap(s1, s2); System.out.println(s1.text + s2.text); // or System.out.println(s1 + s2); since we've already overridden toString() } 

OUTPUT:

 WorldHello 

Java按值传递引用; 在被调用的方法中交换引用对调用者没有影响。 你的字符串是不可变的,因此在调用者可见的swap方法中你无法对它们做任何事情。

但是,如果传递可变对象,您将看到在swap对它们所做的更改将反映在原始对象中:

 public static void main(String[] args) { String a[] = {"hello", "world"}; swap(a); System.out.println(a[0] + " " + a[1]); } static void swap(String[] a) { String t = a[0]; a[0] = a[1]; a[1] = t; } 

这是有效的,因为数组是可变的 (即可以对其状态进行更改),并且因为对原始对象的引用被传递给被调用的函数(按值)。 当swap修改其a的内容时,调用者中a的内容也会被修改,因为它是同一个对象。

 public static void swap(SwapDate[] t) { SwapDate temp;; temp=t[0]; t[0]=t[1]; t[1]=temp; } public static void main(String args[])throws Exception{ SwapDate t[]=new SwapDate[5]; t[0]=new SwapDate(20,8,2015); t[1]=new SwapDate(8,8,2015); System.out.println("(First object)Account Opnening date :"+" "+t[0].date); System.out.println("(second object)Account Opnening date :"+" "+t[1].date); swap(t); System.out.println("After Swap"); System.out.println("(First object)Account Opnening date :"+" "+t[0].date); System.out.println("(second object)Account Opnening date :"+" "+t[1].date); } 

Java严格按价值传递。

让我们从解释的角度修改你的代码如下

 static void swap(String str1, String str2){ String temp = str1; str1=str2; str2=temp; } public static void main(String[] args) { String s1 = "Hello", s2 = "world"; swap(s1, s2); System.out.println(s1 + s2); 

}

S1和S2是保存字符串对象地址的引用。 例如:S1 = 123,S2 = 234

当我们调用swap方法时,会创建S1和S2的副本,并将其分配给str1和str2。

这将是str1 = 123和str2 = 234。

Swap方法只会交换str1和str2中的值。 (str1 = 234 str2 = 123),而S1和S2仍然指向相同的存储位置。

这就是为什么交换没有反映在main方法中的原因。

我们可以交换对象。 它很简单。只需要使用一个虚拟对象来交换两个对象。

示例如下:

  Employee e1=new Employee(); Employee e2=new Employee(); e1.setName("e1"); e2.setName("e2"); System.out.println(e1); System.out.println(e2); Employee temp= new Employee(); temp=e1; e1=e2; e2=temp;