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

Which keyword is written to use a variable declared in one class in the other class?

5 Answers   TCS,


In multiple inheritance , to create sub class object , is there need to create objects for its superclasses??? in java and c++ both. Actually i have some information that is , all available members from its superclasses , memory created in subclass obj , so no need to create object for its superclasses...??? Thanks in Advance

1 Answers  


what is different between oops and c++

0 Answers   IIT,


What are the advantages of polymorphism?

0 Answers  


Why and when is a virtual destructor needed?

5 Answers  


what is abstract class ? when is used in real time ? give a exp

5 Answers  


What language is oop?

0 Answers  


how many types of notations are in java

1 Answers   National University of Modern Languages (NUML),


How to handle exception in c++, For example in a functions i am assigning memory to some variables and also in next instructions in am dividing one variable also. If this functions generates a error while allocating memory to those variable and also while dividing the variable if i divide by zero then catch block how it will identify that this error has cone from perticular instruction

0 Answers  


You have one base class virtual function how will call that function from derived class?

4 Answers  


What are the different forms of polymorphism??

8 Answers   Mantaq, NUC, PCS,


Which is the only operator in C++ which can be overloaded but NOT inherited?

8 Answers  


Categories