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 virtual function?

3 Answers  


i=20;k=0; for(j=1;k-i;k+=j<10?4:3) { cout<<k; } //please comment on the output

0 Answers  


what's the basic's in dot net

0 Answers   informatics,


Explain polymorphism? What r the types of polymorphism? pls give examples?

4 Answers   HCL,


what is the abstract class,interface ,its difference with a programatic eg.? hi,recently i went for an interview they ask me what is abstract class ,interface and its difference I said abstract class contain abstact method ,abstract method is a method with no body.Abstract class cannot be instantiated.Abstract class is a base class it required derived class for the implementation of method. Interface is a syntactical contract that all derived class should follow it define properties ,method,events which are known as member of interface. Then They asked me what is the difference between them. I said abstract class interface 1.abstact class can implement method 1.interface cant 2.abstact class can contain constructor, 2.interface cant destructor 3.abstract class cannot support multiple 3.interface support inheritance etc Then they said some different answer I said dont no. Then they ask me when i should make abstract class for an project and when i should make interface. I said if suppose there is two class which must be having method with different logic then we sholud make abstract class. and if suppose we have two class having method .with different logic then we can make interface . Am i correct with my explaination.if not correct me .please provide me that when should we create abstract class and interface and what is difference .please help me

1 Answers  






what is namespace? what are the uses of namespace?

1 Answers  


What is polymorphism what are the different types of polymorphism?

0 Answers  


write a programe to calculate the simple intrest and compund intrest using by function overlading

0 Answers  


What is the difference between encapsulation and polymorphism?

0 Answers  


which feature are not hold visual basic of oop?

0 Answers   Ignou,


IS IT NECESSARY TO INITIALIZE VARIABLE? WHAT IF THE INSTANCE VARIABLE IS DECLARED final ? IS IT NECESSARY TO INITIALIZE THE final VARIABLE AT THE TIME OF THEIR DECLARATION?

0 Answers  


How to use CMutex, CSemaphore in VC++ MFC

0 Answers   Persistent, TCS,


Categories