Is Java “pass-by-reference” or “pass-by-value”?

Is Java “pass-by-reference” or “pass-by-value”?

A lot of Java programmers wonder if Java is passable by reference or by value. The reason Java is always pass by value is summed up in this article. First, what are the meanings of pass by reference and pass by value?

  • Pass by value: The copied object is supplied to the method after the method parameter values have been transferred to another variable. The copy is used in the technique.
  • Pass by reference: The method receives an alias or reference to the real parameter. The actual parameter is accessed by the method.

The idea of the object reference in Java is frequently the cause of the misunderstanding surrounding these concepts. Although a variable may contain a reference to an object, the object reference is actually a value that indicates the object’s position in memory, therefore technically, Java is always pass by value. As a result, object references are supplied by value.

Primitive and reference data types are both provided by value. Study up on Java’s data types. Because reference data types and basic data types are stored differently, it’s crucial to comprehend memory allocation in Java in addition to data types.

pass by value Example

The following example demonstrates how values are passed in Java.

public class Car{
	private String color;
	public Car() {}	
	public Car(String c) {
		this.color = c;
	}	
	public String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}
}

The following example program uses a generic method, swap(), that swaps two variables. Another method, changeValues(), attempts to change the variable values.

public class Test {
	public static void main(String[] args) {
		Car red = new Car("Red"); // memory reference = 50
		Car blue = new Car("Blue"); // memory reference = 100
		
		swap(red, blue);
		System.out.println("After the swap method executes:");
		System.out.println("`red` color value = " + red.getColor());
		System.out.println("`blue` color value = " + blue.getColor());
		
		changeValues(blue);
		System.out.println("After the changeValues method executes:");
		System.out.println("`blue` color value = " + blue.getColor());		
	}
	// Generic swap method
	public static void swap(Object o1, Object o2){
		Object temp = o1;
		o1 = o2;
		o2 = temp;
	}
	private static void changeValues(Car car) { // car= 100
		car.setColor("Red"); // car= 100
		car = new Car("Green"); // car = 200
		car.setColor("Blue"); // car = 200
	}
}

When you execute the example program, you get the following output:

OutputAfter the swap method executes:
'red' color value = Red
'blue' color value = Blue
After the changeValues method executes:
'blue' color value = Red

The result demonstrates that the color values of the original objects were not changed by the swap() method. Because the swap() function only works with copies of the original object reference values, it helps demonstrate that Java is pass by value. Any programming language can be used to determine whether a method is pass by reference or pass by value using the swap() method test.

swap() Method Explained

When you use the new operator to create an instance of a class, the object is created and the variable contains the location in memory where the object is saved.

Car red = new Car("Red");
Car blue = new Car("Blue");

Here’s a step-by-step breakdown of what happens when the swap() method executes:

  • Suppose that both car objects’ memory addresses are 50 and 100, respectively, and that red and blue are pointing to these locations.
  • Two new object variables, o1 and o2, are produced when the class invokes the swap() function with the red and blue variables as parameters.Additionally, memory locations 50 and 100 are indicated by o1 and o2, respectively.
  • The snippet of code that follows describes what occurs in the swap() method:swap(Object o1, Object o2) and public static void // o2 = 100, o1 = 50 Assign the object reference value of o1 to temp by using the formula temp = 50, o1 = 50, o2 = 100. temp = 50, o1 = 100, o2 = 100 o1 = o2; // assign the object reference value of o2 to o1: o2 = temp; // assign the object

Java is pass by reference, so it’s a common misconception to think that you’re passing the reference because the variables hold the reference to the objects. It is pass-by value, though, because you are providing a value that is a copy of the reference.

changeValues() Method Explained

The next method in the example program changes the color value of the object referenced by the blue variable:

private static void changeValues(Car car) { // car = 100
	car.setColor("Red"); // car = 100
	car = new Car("Green"); // car = 200
	car.setColor("Blue"); // car = 200
}

Here’s a step-by-step breakdown of what happens within the changeValues() method:

  • The blue variable that points to memory address 100 is called by the class using the changeValues() method. A reference that also points to memory address 100 is created in the first line. The item at memory address 100 now has the color value “Red” instead.
  • The new object (with color value “Green”) is created in the second line. Memory location 200 is where the new object is located. The object at memory address 200 is affected by any additional operations performed on the car variable, while the object at memory location 100 is unaffected. The reference made in line 1 is overwritten by the new car variable, and this method no longer makes the car reference available.
  • The old object that blue references at memory address 100 remains unaltered, but the third line adds “Blue” as the color value of the new car object at memory location 200. This explains why the example program output’s last line, which represents the change from line 1, prints blue color value = Red.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *