Say x & y are the integer variables. Dize x & y são as variáveis do tipo inteiro. The challenge is to swap them without using a temporary variable. O desafio é o de swap-los sem utilizar uma variável temporária.

The solution is as simple as the problem itself: A solução é simples como o problema em si:
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. Update 1: Jack e Alexey apontam para um erro na minha solução que já foi corrigido. Please see their comments below. Por favor, veja abaixo as suas observações.

The simplicity of the solution appeals to me. A simplicidade da solução agrada-me. It clearly demonstrates the meaning of assignment operator ( “=” ). Ela demonstra claramente o significado da atribuição operador ( "=").

What if they are Strings? E se eles são Strings?
Note: You can use String methods in Java API. Nota: Você pode usar métodos String em Java API.

Here is the solution: Aqui está a solução:
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? Você pode ver a semelhança?

Can you provide a simpler solution to either of the above? Vocês podem fornecer uma solução mais simples para qualquer um dos acima?

Update 2: Robert just did (see his comment below). Update 2: Robert fez (ver seu comentário abaixo). He pointed out that the String solution doesn’t work when one string is contained in another. Ele salientou que a solução String não funciona quando uma string está contida no outro. His solution is: Sua solução é:

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 ());