Say x & y are the integer variables. Di 'x & y sono le variabili intero. The challenge is to swap them without using a temporary variable. La sfida è quella di swap di loro senza utilizzare un temporaneo variabile.

The solution is as simple as the problem itself: La soluzione è semplice come il problema stesso:
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. Aggiornamento 1: Jack e Alexey rilevato un errore nel mio soluzione che da allora è stato corretto. Please see their comments below. Si prega di consultare le loro osservazioni qui di seguito.

The simplicity of the solution appeals to me. La semplicità della soluzione appelli a me. It clearly demonstrates the meaning of assignment operator ( “=” ). Esso dimostra chiaramente il significato di assegnazione ( "=").

What if they are Strings? Che cosa se sono stringhe?
Note: You can use String methods in Java API. Nota: È possibile utilizzare metodi String in Java API.

Here is the solution: Ecco la soluzione:
x = x + y; x = x + y;
y = x.substring(0, x.indexOf(y)); x.substring y = (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? Si può vedere la somiglianza?

Can you provide a simpler solution to either of the above? Si può fornire una soluzione più semplice, di una delle suddette?

Update 2: Robert just did (see his comment below). Aggiornamento 2: Robert ha fatto solo (si veda il suo commento qui sotto). He pointed out that the String solution doesn’t work when one string is contained in another. Egli ha sottolineato che la soluzione Stringa non funziona quando una stringa è contenuta in un altro. His solution is: La sua soluzione è:

x = x + y; x = x + y;
y = x.substring(0, x.length() - y.length()); x.substring y = (0, x.length () - y.length ());
x = x.substring(y.length()); x = x.substring (y.length ());