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

i ahve v low % in 12th n BSC which is aroun 50 coz science was imposed on me......nw m doin MCA n my aggregate in above 74%,what shud i say if asked about low previous percentage??????

4 Answers  


How long to learn object oriented programming?

0 Answers  


Why do we use polymorphism in oops?

0 Answers  


What do you mean by variable?

0 Answers  


What is for loop and its syntax?

0 Answers  






what is the drawback of classical methods in oops?

0 Answers  


What is an interface in oop?

0 Answers  


WHAT IS THE DIFFERENCE BETWEEN OBJECT BASED & OBJECT ORIENTD PROGRAMMING LANGUAGE.(GIVE AT LIST 4 PIONT)

1 Answers   TCS,


What is abstraction example?

0 Answers  


What is the real time example of inheritance?

0 Answers  


String = "C++ is an object oriented programming language.An imp feature of oops is classes and objects".Write a pgm to count the repeated words from this scenario?

0 Answers   HCL,


What is the point of polymorphism?

0 Answers  


Categories