how to swap to variables without using thrid variable in java?

Answers were Sorted based on User's Feedback



how to swap to variables without using thrid variable in java?..

Answer / igor polivanyi

It’s pretty simple:

int a = 12;
int b = 34;

a = a + b;
b = a - b;
a = a - b;

And for real cowboys, it could be simplified further:

a -= (b = (a += b) - b);

Is This Answer Correct ?    8 Yes 0 No

how to swap to variables without using thrid variable in java?..

Answer / hemant verma

a=a^b^(b=a);

Hemant Verma
FameofLight@gmail.com

Is This Answer Correct ?    2 Yes 0 No

how to swap to variables without using thrid variable in java?..

Answer / sibaram

int a=5;
int b=6;
b=a*b/(a=b);

Is This Answer Correct ?    2 Yes 0 No

how to swap to variables without using thrid variable in java?..

Answer / mani

int x = 15;
int y = 20;
System.out.println(x+" "+y);
x ^= y;
y ^= x;
x ^= y;
System.out.println(x+" "+y);

Is This Answer Correct ?    2 Yes 1 No

how to swap to variables without using thrid variable in java?..

Answer / jack

cool code fame

Is This Answer Correct ?    0 Yes 1 No

Post New Answer

More OOPS Interview Questions

What is a unary operator?

5 Answers  


Write a program to get the binary tree.

3 Answers   ABC,


suppose A is a base class and B is the derved class. Both have a method foo which is defined as a virtual method in the base class. You have a pointer of classs B and you typecast it to A. Now when you call pointer->foo, which method gets called? The next part of the question is, how does the compiler know which method to call?

3 Answers   EA Electronic Arts,


how to get the oracle certification? send me the answer

0 Answers   Oracle,


What is the difference between the C & C++?

9 Answers   HCL,






when my application exe is running nad i don't want to create another exe what should i do

2 Answers   HCL,


Child cObj = new Parent() Wahts the output ?

8 Answers   Patni, TCS,


What is ambiguity in inheritance?

0 Answers  


1. Strong name 2. how to prevent a class from being inherited 3. delegates 4. default modifier for interface 5. default modifier for class 6. base class for exception 7. diff bet trigger and view in sql 8. how to exchange values from one page to another page 9. can multiple catch block ll be executed at same time 10. can u store different data types in an array & array list 11. when we ll use trigger 12. try,catch,finally usage

2 Answers  


What is the differances between a abstract calss and interface

5 Answers   Aviva, Symphony,


what is the usage of clas templates

5 Answers  


What are the valid types of data that the main () can return in C/C++ language

3 Answers  


Categories