Ini review lagi tentang Java! Check this out.
Question: Does Java pass by reference or pass by value?
Answer:
Before answering the question, let define which are pass-by-reference and pass-by-value means.
Pass-by-value
The actual parameter (or argument expression) is fully evaluated and the resulting value is copied into a location being used to hold the formal parameter’s value during method/function execution. That location is typically a chunk of memory on the runtime stack for the application (which is how Java handles it), but other languages could choose parameter storage differently.
Pass-by-reference
The formal parameter merely acts as an alias for the actual parameter. Anytime the method/function uses the formal parameter (for reading or writing), it is actually using the actual parameter.
Source: http://javadude.com/articles/passbyvalue.htm
But, fact about Java explains that:
Truth #1: Everything in Java is passed by value. Objects, however, are never passed at all.
That needs some explanation – after all, if we can’t pass objects, how can we do any work? The answer is that we pass references to objects. That sounds like it’s getting dangerously close to the myth, until you look at truth #2:
Truth #2: The values of variables are always primitives or references, never objects.
This is probably the single most important point in learning Java properly. It’s amazing how far you can actually get without knowing it, in fact – but vast numbers of things suddenly make sense when you grasp it.
Source: http://www.yoda.arachsys.com/java/passing.html
According to these two facts, we can conclude that:
Java does manipulate objects by reference, and all object variables are references. However, Java doesn’t pass method arguments by reference; it passes them by value.
Source: http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html
To give clearer answer, let examine this example. In this example I create three classes below:
1. Class ReferenceTest is the blueprint of an object which we use as example of swapped object.
public class ReferenceTest {
int myRef;
public ReferenceTest(int myInput){
this.myRef = myInput;
}
public int getMyRef(){
return myRef;
}
public void setMyRef(int myInput){
this.myRef = myInput;
}
}
2. Class SwapTest which contain method to do swapping test.
public class SwapTest {
public static void swaping(ReferenceTest rt1, ReferenceTest rt2){
System.out.println();
System.out.println(“=======================================”);
System.out.println(“This is what happened in swap process:”);
System.out.println(“Before swapping: “);
System.out.println(“Value of firstRt: ” + rt1.getMyRef());
System.out.println(“Value of secondRt: ” + rt2.getMyRef());
ReferenceTest tempRt;
tempRt = rt1;
rt1 = rt2;
rt2 = tempRt;
System.out.println();
System.out.println(“After swapping: “);
System.out.println(“Value of firstRt: ” + rt1.getMyRef());
System.out.println(“Value of secondRt: ” + rt2.getMyRef());
System.out.println(“=======================================”);
}
}
3. Class MainTest is the main class.
public class MainTest {
public static void main(String[] args) {
ReferenceTest firstRt;
ReferenceTest secondRt;
firstRt = new ReferenceTest(5);
secondRt = new ReferenceTest(10);
System.out.println(“Before swapping: “);
System.out.println(“Value of firstRt: ” + firstRt.getMyRef()); System.out.println(“Value of secondRt: ” + secondRt.getMyRef()); SwapTest.swaping(firstRt, secondRt);
System.out.println();
System.out.println(“After swapping: “);
System.out.println(“Value of firstRt: ” + firstRt.getMyRef()); System.out.println(“Value of secondRt: ” + secondRt.getMyRef());
}
}
Result of the application is as shown below:
Before swapping:
Value of firstRt: 5
Value of secondRt: 10
=======================================
This is what happened in swap process:
Before swapping:
Value of rt1: 5
Value of rt2: 10
After swapping:
Value of rt1: 10
Value of rt2: 5
=======================================
After swapping:
Value of firstRt: 5
Value of secondRt: 10
By the first, application will create two instances of ReferenceTest class which are firstRt and secondRt objects. Java will create two Objects which type is ReferenceTest and two variables whose value is a pointer to those two objects.
Figure1. Java will create two objects and two variables.
So, when we attempt to execute these statements in main method:
System.out.println(“Before swapping: “);
System.out.println(“Value of firstRt: ” + firstRt.getMyRef());
System.out.println(“Value of secondRt: ” + secondRt.getMyRef());
Java will give values of ReferenceTest objects on which those two variables pointed. The result of these statements is:
Before swapping:
Value of firstRt: 5
Value of secondRt: 10
When we pass these two variables (firstRt and secondRt) into swaping method, Java will create two variables to collect those two variables which were passed.
Figure2. Java will create two variables in swaping method.
So, when we attempt to execute these statements:
System.out.println(“Before swapping: “);
System.out.println(“Value of firstRt: ” + rt1.getMyRef());
System.out.println(“Value of secondRt: ” + rt2.getMyRef());
These statements will give same result as statements in Main method, since these two variables contain value of pointer which is pointed into same objects which also pointed by firstRt and secondRt in Main method. The result of these statements is:
Before swapping:
Value of rt1: 5
Value of rt2: 10
When we swap rt1 and rt2, means we swap pointer value contained by them. Even though they were swapped, actually firstRt and secondRt are not swapped (since Java passes the reference by value).
So, when we attempt to execute these statements in swaping method after swapping process:
System.out.println(“After swapping: “);
System.out.println(“Value of firstRt: ” + rt1.getMyRef());
System.out.println(“Value of secondRt: ” + rt2.getMyRef());
This will give output as shown below:
After swapping:
Value of rt1: 10
Value of rt2: 5
Figure3. After swap process, rt1 and rt2 is swapped while firstRt and secondRt isn’t.
Otherwise, when we execute these statements in Main method:
System.out.println(“After swapping: “);
System.out.println(“Value of firstRt: ” + firstRt.getMyRef());
System.out.println(“Value of secondRt: ” + secondRt.getMyRef());
This will give output as shown below:
After swapping:
Value of firstRt: 5
Value of secondRt: 10







Recent Comments