Fundamentals 7 min read

Swapping Two Variables in Java Without a Temporary Variable

This tutorial explains how to exchange the values of two Java variables without using a temporary variable, covering three techniques—arithmetic addition/subtraction, multiplication/division, and bitwise XOR—along with complete code examples, step‑by‑step explanations, output results, and discussion of each method’s advantages, limitations, and appropriate use cases.

Cognitive Technology Team
Cognitive Technology Team
Cognitive Technology Team
Swapping Two Variables in Java Without a Temporary Variable

Swapping two variables is a common task in programming. This guide shows how to do it in Java without a temporary variable.

Methods covered:

Arithmetic addition and subtraction

Multiplication and division

Bitwise XOR

Method 1: Arithmetic

package com.mkyong;
public class SwapWithoutTemp {
    public static void main(String[] args) {
        int a = 10, b = 20;
        System.out.println("Before: a = " + a + ", b = " + b);
        a = a + b;
        b = a - b;
        a = a - b;
        System.out.println("After: a = " + a + ", b = " + b);
    }
}

Explanation of each step and a note about possible integer overflow when the sum exceeds Integer.MAX_VALUE .

Method 2: Multiplication/Division

package com.mkyong;
public class SwapUsingMultiplication {
    public static void main(String[] args) {
        int a = 10, b = 20;
        System.out.println("Before: a = " + a + ", b = " + b);
        a = a * b;
        b = a / b;
        a = a / b;
        System.out.println("After: a = " + a + ", b = " + b);
    }
}

This method works only when neither variable is zero and may overflow for large numbers.

Method 3: Bitwise XOR

package com.mkyong;
public class SwapUsingXOR {
    public static void main(String[] args) {
        int x = 10, y = 20;
        System.out.println("Before: x = " + x + ", y = " + y);
        x = x ^ y;
        y = x ^ y;
        x = x ^ y;
        System.out.println("After: x = " + x + ", y = " + y);
    }
}

The XOR technique needs no extra memory, avoids overflow, but is less readable and works only with integer types.

Advantages of XOR: no temporary variable, no overflow risk, fast bit operation. Disadvantages: lower readability, not suitable for floating‑point numbers, harder to debug.

Choosing a method depends on the data range and readability requirements; for most cases the arithmetic method is simple, while XOR is useful when memory usage is critical.

Reference: https://mkyong.com/java/java-swap-two-variables-without-a-temp-variable (tested with Java 21).

JavaArithmeticBitwise OperationsProgramming Tutorialvariable swappingxor
Cognitive Technology Team
Written by

Cognitive Technology Team

Cognitive Technology Team regularly delivers the latest IT news, original content, programming tutorials and experience sharing, with daily perks awaiting you.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.