Java swapping and pass by value/ref

Mike Sun
4 min readApr 3, 2019

Synopsis: Simple application of swap and why java’s pass-by-value/ref is confusing.

This is just a simple illustration of the concept of swapping.

//Say we have 2 var
int x=1,y=3;
//some code...output:
>>x=3
>>y=1

Sometimes we may want to introduce a ‘temp’ variable inside our code for easier tracking of values, and probably for readability too.

Below is a simple case of Collatz Problem in simple Java logic, not the most efficient way, but solves the problem in a relatively reasonable time up till over a million for n. In this case, we want to reverse engineer to find the value of n that gives the longest chain.

Problem description:

  1. Any even number, n, will be subjected to n/2;

2. Any odd number, n, will be subjected to 3n+1;

Hypothesis: for all n is element of positive integer, the end result will always be 1.

Example: if we start with n=13

13->40->20->10->5->16->8->4->2->1//Collatz problem:
int counter = 0;
int maxCounter = 0;
long temp = 0;
long temp2 = 0;
for (long n=3;n<100;n+=2) {
temp=n;
while(n!=1){
if (n%2==0) {
n=n/2;
counter++;
}
else {
n=3*n+1;
counter++;
}
}

if (counter>maxCounter) {
maxCounter = counter;
temp2 = temp;
}
n=temp;
counter = 0;
}
System.out.println(temp2);

The code is pretty simple to read, apart from the weird ‘temp’ variables intertwined into the code. The first temp is because I wanted to keep track of n before it goes into the while loop, since at the end of the while loop, n will always be 1(if not we will be stuck in an infinite while loop). The for loop adds 2 to n each time, and if n is always 1, we will be stuck in an infinite for loop of n=3(this is a rare instance of infinite for loop >.<). To counteract this bug, we revert n=temp, which has the value of the original n, before the while loop. However, our question requires us to print out the n which give the longest chain, i.e. the largest counter.

This brings us to another bug: we do not know which n gives the maxCounter in the for loop. We cannot simply print n, since that will print the last n only. To solve this, we introduce a temp2 to store the value of n when maxCounter is reached.

The above application is a simple taste of using the concept of swapping in one method, which works perfectly fine and logical. However, things get interesting when multiple methods and objects come into play.

For instance, try this code below:

public class multipleMethod{
public static void main(String[] args) {
int x=1,y=3;
swap1(x,y);
System.out.println(x);
}
public static void swap1(int a, int b){
int temp = a;
a = b;
b = temp;
}
}

Guess what’s the output! It will actually be 1.

Hang on a sec, shouldn’t x and y swap? The logic seems pretty much like in the application example in part 1 !

Well, this is due to how java handles memory. Each individual method will have its own memory allocated, known as a stack. When you pass var in one stack to another, the value is copied over. The original value remain independent of what the other method perform on the copied value.

This is known as pass-by-value in CS lingo. However, before you conclude that Java is indeed pass-by-value only, let’s look at another example, this time with objects.

note: arrays are objects in java

public static void swap2(int[] a, int i, int j) { 
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
public static void main(String[] args) {
int [] arr = {1,2,3,4,5,6,7};
swap3(arr,0,3);
System.out.println(Arrays.toString(arr));
}console:
>>[4, 2, 3, 1, 5, 6, 7]

The above example defies what we have learnt so far, and it seems that java uses pass-by-reference instead. This is because objects in java live in a heap and thus can be referenced. When you pass the arguments in main to the parameter of swap3, the address of the same object is used. Hence, when modified, it will affect the original variables memory.

I hope you have learnt something today with me! Happy coding :)

--

--

Mike Sun
0 Followers

Enigmatically simple. Aspiring photographer and technopreneur based in Singapore. www.linkedin.com/in/mike-sun