Home  >  Article  >  Java  >  Does Java Pass Objects by Value or Reference?

Does Java Pass Objects by Value or Reference?

Barbara Streisand
Barbara StreisandOriginal
2024-11-21 06:14:10396browse

Does Java Pass Objects by Value or Reference?

Java: Pass by Value vs. Pass by Reference

In Java, misconceptions often arise regarding object passing mechanisms. This article will delve into the distinction between two code snippets, shedding light on the fundamental principles of Java's parameter passing behavior.

Code A vs. Code B

Code A:

Foo myFoo;
myFoo = createfoo();

public Foo createFoo() {
   Foo foo = new Foo();
   return foo;
}

Code B:

Foo myFoo;
createFoo(myFoo);

public void createFoo(Foo foo) {
   Foo f = new Foo();
   foo = f;
}

Understanding Java's Parameter Passing

Contrary to popular belief, Java utilizes pass by value, not by reference. This means that when an object is passed as an argument, a copy of its reference is created rather than a direct reference to the original.

Code A Explanation

In the first line, myFoo is declared but not assigned. The second line calls the createFoo method, which returns a new Foo object that is assigned to myFoo. This assignment creates a new object that is distinct from the one created within the method.

Code B Explanation

In contrast, Code B passes the myFoo reference as an argument to the createFoo method. Within the method, a new Foo object is created and assigned to the foo parameter. Subsequently, foo is assigned to a, which is a local variable within the method. This reassignment effectively modifies the local reference foo, not the original reference myFoo.

Example for Clarity

To illustrate the concepts, consider the following class:

public class Foo {
   private String attribute;

   public void setAttribute(String attribute) {
      this.attribute = attribute;
   }
}

Main Class:

public class Main {
   public static void main(String[] args) {
      Foo f = new Foo("f");
      changeReference(f); // Attempt to change reference
      modifyReference(f); // Attempt to modify object attribute
   }

   public static void changeReference(Foo a) {
      Foo b = new Foo("b");
      a = b;
   }

   public static void modifyReference(Foo c) {
      c.setAttribute("c");
   }
}

After the main method creates an instance of Foo and assigns it to f, it calls changeReference, which attempts to reassign the a reference to a different object. However, this does not alter the f reference.

Contrarily, modifyReference modifies the attribute of the object referenced by c, which is the same object as f. This demonstrates that reference passing creates a copy of the reference, not the object itself.

Conclusion

Java's pass by value approach ensures object immutability by protecting original objects from unintentional modifications outside their scope. This principle promotes encapsulation and facilitates the creation of robust and predictable code.

The above is the detailed content of Does Java Pass Objects by Value or Reference?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn