Understanding Java pass by value


I think most of the people they are confused with this concept because it is appearing in different angles. First thing I have to say that Java is pass by Value not pass by reference. I’ll show the example to clear the concept.

First of all I’ll show the sample code, then you can see the confused point that you will drive to think that java is pass by reference. you can checkout the full source here.

package sample.passby.value;

/**
 * Point class
 * 
 * @author malalanayake
 *
 */
public class Point {
	private int x;
	private int y;

	public Point(int x, int y) {
		this.x = x;
		this.y = y;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

}
package sample.passby.value;

/**
 * Demonstration of Java pass by value
 * 
 */
public class App {
	public static void main(String[] args) {
		int xOrginal = 0, yOriginal = 0;
		System.out.println("Original Coordinates:" + xOrginal + "," + yOriginal);
		App.getNextCoordinates(xOrginal, yOriginal);
		System.out.println("After modification of Coordinates:" + xOrginal
				+ "," + yOriginal);

		Point pOriginal = new Point(0, 0);
		System.out.println("Original Point:" + pOriginal.getX() + ","
				+ pOriginal.getY());
		App.getNextCoordinates(pOriginal);
		System.out.println("After modification of Point:" + pOriginal.getX()
				+ "," + pOriginal.getY());

	}

	public static void getNextCoordinates(int x, int y) {
		x = 12;
		y = 15;
	}

	public static void getNextCoordinates(Point point) {
		point.setX(12);
		point.setY(15);
	}
}

Output:
Original Coordinates:0,0
After modification of Coordinates:0,0
Original Point:0,0
After modification of Point:12,15

So you can see in the above example if I pass the primitives then it is not going to change the values. But if I pass the object type then if i change something on that it will reflect. The understanding point is Java every time pass by value and if it is primitive then it is just pass the value to the given argument and nothing will reflect. See the following image for understanding.
PassByValue (1)

But if we pass the Object type then java pass the “reference by value”. This is the tricky point that we need to understand.
PassByValue (2)

So you can see if we pass some Object type to some function actually its going to pass the pointer to that Object. That means if we change some thing on that it will be reflect on that. But be aware about this, You cannot change the pointer which is not in the scope. As an example if you do following coding in getNextCoordinates(Point point) method it will not reflecting anything on the original object because it is going to change the pointer of the object which is under that scope not the original object that passed to the function.

public static void getNextCoordinates(Point point) {
                point = new Point(12,15);
}

Leave a comment

Create a free website or blog at WordPress.com.

Up ↑