Is Java pass by value or pass by reference?

What are good implementation for swap() method in Java(pass by reference)?

  • void swap(int a,int b) I try to implement this method in Java,never works,how to pass by reference in Java

  • Answer:

    The easiest way to pass by reference is to put the parameters in an array, and pass the array. So your method would look something like: void swap(int[] ab) {   int temp = ab[0];   ab[0] = ab[1];   ab[1] = temp; } You can't do this with a method signature of swap(int a, int b) because by definition, that's pass-by-value.

Rich Freedman at Quora Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.