Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8212831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:54:12+00:00 2026-06-07T10:54:12+00:00

I want to swap the value of two integer variables in java using the

  • 0

I want to swap the value of two integer variables in java using the XOR operator.

This is my code:

int i = 24;
int j = 17;

i ^= j;
j ^= i;
i ^= j;

System.out.println("i : " + i + "\t j : " + j);

It will work fine but the following equivalent code doesn’t work:

int i = 24;
int j = 17;

i ^= j ^= i ^= j;

System.out.println("i : " + i + "\t j : " + j);

Output is like this:

i : 0    j : 24

First variable is zero! What’s wrong with Java?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T10:54:13+00:00Added an answer on June 7, 2026 at 10:54 am

    According to Java specification (Java 7 specification), Section 15.26.2 (page 529).

    A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T) ((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once.

    According to Section 15.7 Evaluation Order (Page 423) (emphasis mine):

    15.7 Evaluation Order

    The Java programming language guarantees that the operands of operators appear to be evaluated in a specific evaluation order, namely, from left to right.

    15.7.1 Evaluate Left-Hand Operand First

    The left-hand operand of a binary operator appears to be fully evaluated before any part of the right-hand operand is evaluated.

    If the operator is a compound-assignment operator (§15.26.2), then evaluation of the left-hand operand includes both remembering the variable that the left-hand operand denotes and fetching and saving that variable’s value for use in the implied binary operation.

    If evaluation of the left-hand operand of a binary operator completes abruptly, no part of the right-hand operand appears to have been evaluated.

    Described in more details in Section 15.26.2 (page 529):

    If the left-hand operand expression is not an array access expression, then:

    • First, the left-hand operand is evaluated to produce a variable. [trimmed]

    • Otherwise, the value of the left-hand operand is saved and then the right-hand operand is evaluated. [trimmed]

    • Otherwise, the saved value of the left-hand variable and the value of the right-hand operand are used to perform the binary operation indicated by the compound assignment operator. [trimmed]

    • Otherwise, the result of the binary operation is converted to the type of the left- hand variable, subjected to value set conversion (§5.1.13) to the appropriate standard value set (not an extended-exponent value set), and the result of the conversion is stored into the variable.

    An example in the documentation

    Example 15.26.2-2. Value Of Left-Hand Side Of Compound Assignment Is Saved Before Evaluation Of Right-Hand Side

      class Test {
          public static void main(String[] args) {
              int k = 1;
              int[] a = { 1 };
              k += (k = 4) * (k + 2);
              a[0] += (a[0] = 4) * (a[0] + 2);
              System.out.println("k==" + k + " and a[0]==" + a[0]);
          }
      }
    

    So the expression in the question is re-written and grouped as:

    i = i ^ (j = j ^ (i = i ^ j));
    

    Left-hand operands are evaluated:

    i = 24 ^ (j = 17 ^ (i = 24 ^ 17));
        **
    

    Since the value of i is not "updated" as expected, it will cause the value of i to get 0 when 24 is swapped to j.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In most languages, if you want to swap two variables, it's something like: var
I have two integer variables i and j and I want to make a
I want to swap two fragment in an activity via animation.Suppose PageA is for
want to know why String behaves like value type while using ==. String s1
I want to swap the values from two rows in a table. I have
I want to add swap functionality to two existing C++ classes. One class inherits
I want to swap values of two rows of a particular column in SQLite
Possible Duplicate: Is it possible to write swap method in Java? Given two values
System.Threading.Interlocked.CompareExchange operator provides atomic (thus thread-safe) C# implementation of the Compare-And-Swap operation. For example
I want to discuss my problem clearly. I have a function to Swap value

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.