---
title: "Pass by Value vs. Pass by Reference in Java"  
description: "All arguments to methods are passed by value, meaning a copy of the actual value is passed to the method."  
author: "Ashutosh Patel"  
published: 2025-03-20  
updated: 2025-03-20  
canonical: https://www.mindstick.com/articles/338817/pass-by-value-vs-pass-by-reference-in-java  
category: "java"  
tags: ["java"]  
reading_time: 2 minutes  

---

# Pass by Value vs. Pass by Reference in Java

In Java, all [arguments](https://answers.mindstick.com/qa/92535/what-are-the-different-types-of-arguments) to [methods](https://www.mindstick.com/interview/2253/what-are-difference-between-get-and-post-methods) are `pass by value`, which means that a copy of the actual value is passed to the method. However, how this behaves depends on whether the argument is a **primitive type** or a **[reference type](https://www.mindstick.com/interview/235/what-is-difference-between-value-types-and-reference-types-in-vb-dot-net-or-c-sharp-value-types-vs-reference-types)**.

**[Pass by Value](https://www.mindstick.com/forum/34600/pass-by-value-and-reference) (Primitive Data Types)**

When a **primitive type** (e.g., `int`, `double`, `char`, etc.) is passed to a method, and Java passes a copy of the actual value. Any modifications inside the method do not affect the original variable.

## Example

```java
class Test {
   static void modifyValue(int x) {
       x = 50; // Changes only the copy, not the original
       System.out.println("Value in method: " +x); // Output: 50
   }
   public static void main(String[] args) {
       int num = 10;
       modifyValue(num);
       System.out.println("After method call: " + num); // Output: 10
   }
}
```

**[Explanation](https://answers.mindstick.com/blog/94/neural-network-simple-explanation-with-real-life-examples)**

- The method `modifyValue(int x)` receives a copy of `num`.
- Any changes to `x` inside the method do not affect `num` in `main()`.

## Pass by Reference (Reference Types)

When an **[object reference](https://www.mindstick.com/forum/1739/object-reference-is-not-set-to-an-instance-of-an-object-error-in-my-code)** is passed, Java still passes it **by Reference**, which means that a copy of the reference ([memory](https://answers.mindstick.com/qa/104862/explain-oracle-in-memory-column-store) address) is passed. However, since both the original and copied references point to the same object, modifications inside the method affect the original object.

## Example

```java
class Test {
    static void modifyObject(Person p) {
        p.name = "Balaji"; // Modifies the actual object
    }

    public static void main(String[] args) {
        Person person = new Person();
        person.name = "Jagdamb";

        modifyObject(person);
        System.out.println("After method call: " + person.name); // Output: John
    }
}

 class Person {
    String name;
}
```

## Explanation

- The `modifyObject(Person p)` [method gets](https://answers.mindstick.com/qa/33377/which-activity-lifecycle-method-gets-called-whenever-the-screen-of-the-device-turns-off-an-then-turns-on) a **copy of the reference**.
- `person` (in `main`) and `p` (in `modifyObject`) both point to the **same object**.
- Changing `p.name` also affects `person.name`.

## Key Points

- Java always passes arguments by value.
- Primitive values ​​are copied, so changes do not affect the original variable.
- Object references are copied, but both copies point to the same object, so changes affect the original object.
- Java does not [support](https://yourviews.mindstick.com/view/81072/pakistan-wants-enmity-not-support-from-india-during-coronavirus-pandemic) true "pass by reference" like C++.

Thanks!

Also, Read: [Abstraction and Interfaces in Java](https://www.mindstick.com/articles/338811/abstraction-and-interfaces-in-java)

---

Original Source: https://www.mindstick.com/articles/338817/pass-by-value-vs-pass-by-reference-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
