Say x & y are the integer variables.说x与y是整数的变数。 The challenge is to swap them without using a temporary variable.所面临的挑战是要交换他们不使用临时变量。

The solution is as simple as the problem itself:解决的办法是那么简单的问题本身:
x = x + y; x =用X + y ;
y = x - y;为Y = x -y ;
x = x - y; x = x -y ;

Update 1: Jack and Alexey pointed out a typo in my solution which has since been corrected.更新1 :杰克和阿列克谢指出,错字在我的解决方案,其中已被纠正。 Please see their comments below.请参阅他们的意见如下。

The simplicity of the solution appeals to me.简单的解决方案呼吁我。 It clearly demonstrates the meaning of assignment operator ( “=” ).它清楚地表明所指的转让的营办商( “ = ” ) 。

What if they are Strings?如果他们是字符串呢?
Note: You can use String methods in Java API.注意:您可以使用字符串的方法的Java API 。

Here is the solution:这里是解决方法:
x = x + y; x =用X + y ;
y = x.substring(0, x.indexOf(y));为Y = x.substring ( 0 , x.indexof (元Y ) ) ;
x = x.substring(x.indexOf(y) + y.length()); x = x.substring ( x.indexof (元Y ) + y.length ( ) ) ;

Can you see the similarity?你能看到的相似性呢?

Can you provide a simpler solution to either of the above?你能提供一个更简单的解决办法上述任一?

Update 2: Robert just did (see his comment below).更新2 :罗伯特只是没有(见他的评论下文) 。 He pointed out that the String solution doesn’t work when one string is contained in another.他指出,该字符串的解决办法行不通时,一个字符串是载于另一个。 His solution is:他的解决办法是:

x = x + y; x =用X + y ;
y = x.substring(0, x.length() - y.length());为Y = x.substring ( 0 , x.length ( ) -y .length( ) ) ;
x = x.substring(y.length()); x = x.substring ( y.length ( ) ) ;