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 opps?why it is use in programming language?

2 Answers   Wipro,


What are constructors in oop?

0 Answers  


what is the realstic modeling?

1 Answers  


write a c++ program to find maximum of two numbers using inline functions.

3 Answers   IBM,


What language is oop?

0 Answers  






Difference between vector and array

2 Answers  


How do you achieve polymorphism?

0 Answers  


What is function overloading?,describe it with the example.

5 Answers  


Why polymorphism is used in oops?

0 Answers  


what is the difference between javap and jad utility

1 Answers   Wipro,


difference between abstraction and encapsulation with progarammatic eg. hi,just recently i went for an interview .The interviewer asked what is the difference between abstraction and encapsulation with programmatic eg. I gave the answer as encapsulation mean hiding the relevant data which is not useful for the user, eg a electric fan .hiding the information how the electricity is converted into machanical energy. abtraction showing only the relevant data to the user eg electric fan. it look ,its color ,it design etc only relevant data. Then the interviewer asked me, give me some programmic eg .I Said Let assume a web form having control like textbox,button etc. The user can view textbox,button etc this is the eg of abstraction and when the user click on the button how he is redirected is not known by the user is the eg of the encapsulation. Am I Correct .was the answer given by me is perfect .now i am planing to go for an another interview should i give the same answer.IF not please suggest me a better answer.with some good eg Please help

1 Answers  


What is multilevel inheritance?

0 Answers  


Categories