Hey Why is it not possible in Java to write a swap routine t

Hey,

Why is it not possible in Java to write a swap routine that swaps two variables of the same primitive type?

Solution

Java passes method argument as reference by value and hence a direct swap is not possible (this is possible in language like C where you can pass reference instead of value and get swapped output).

That being said here is how you can do swap of primitive for example int:

Hey, Why is it not possible in Java to write a swap routine that swaps two variables of the same primitive type?SolutionJava passes method argument as reference

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site