CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Java > Pass by Value vs. Pass by Reference in Java

Java Tutorial

  • Introduction
    • What is Java
    • History Of Java
    • Install Java
    • What is JVM
    • JDK vs JRE vs JVM
    • Java Bytecode
    • OOP vs POP
    • Compile and Run Java
  • Tokens, Expressions and Control Structures
    • Primitive data types
    • Integers
    • Floating Points
    • Characters
    • Booleans
    • User Defined Data Type
    • Declarations
    • Constants
    • Identifiers
    • Literals
    • Type Conversion and Casting
    • Variables
    • Default Variable Initialization
    • Command Line Arguments
    • Arrays of Primitive Types
    • Comment Syntax
    • Garbage Collection
    • Expressions
    • Operators
    • Arithmetic Operator
    • Bitwise and Shift Operator
    • Comparison or Relational Operators
    • Logical Operators
    • Assignment Operators
    • Ternary Operator
    • Increment and Decrement Operator
    • Control Statements
  • OOP Concepts
    • Class and Object
    • Create Class Instance
    • Method
    • Abstraction
    • Encapsulation
    • this keyword
    • Constructor
    • Pass by Value
    • Access Modifier/Control
    • Polymorphism
    • Method Overloading vs Method Overriding
    • Recursion
    • Nested and Inner Class
  • Inheritance and Packaging
    • Inheritance
    • extends Keyword
    • super Keyword
    • Object Class
    • Abstract class
    • Final Class
    • Java Package
    • Interface
  • Handling Error/ Exceptions
    • What is Exception
    • Exception Handling Keywords
    • Common Java Errors
    • User Defined Exception
    • Throwing and re-throwing Exception
    • finally Block
  • Strings
    • Java String Tutorial
  • Threads
    • Introduction
    • Create Thread
    • Thread Lifecycle
    • Thread Priority
    • Thread Synchronization
    • Inner Thread Communication
    • Thread Deadlock
  • IO and Streams
    • java.io Package
    • Files and Directories
    • Byte Stream
    • Character Stream
    • Console Input and Output
    • Serializable and Deserializable
  • Core Packages
    • java.lang Package
    • Math
    • Wrapper Classes
    • java.lang.Number
    • Double
    • Float
    • Integers
    • java.lang.Byte
    • java.lang.Short
    • java.lang.Long
    • java.lang.Character
    • java.lang.Boolean
    • java.util package
    • Vector Class
    • Stack Class
    • Dictionary Class
    • Hashtable
    • Enumeration or Enum
    • Generate Random Number
  • Holding Collection of Data
    • Arrays
    • Map
    • List
    • Set
    • Collection Interface
    • Collections Class
    • ArrayList
    • HashSet
    • TreeSet
    • Comparator
  • Java Bean
    • What is Java Bean
    • Advantages and Disadvantages of Java Bean
    • Java Beans API
    • Introspection
    • Java Bean Properties
    • Bound and Constrained Properties
    • BeanInfo Interface
    • Customizers
    • Java Beans Persistence
    • BeanDescriptor
  • Home

Pass by Value vs. Pass by Reference in Java

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Last updated: July 1, 2026 · 3 min read · 0 Comments
Share: in X

Java developers often debate whether the language uses pass by value or pass by reference. The answer is clear: Java is strictly pass by value. However, confusion arises when working with objects. Let’s break down how Java handles parameter passing for primitives and objects, debunk myths, and clarify common misunderstandings.

Table of Contents

  • Key Definitions
    • 1. Pass by Value
    • 2. Pass by Reference
  • Java’s Strict Pass by Value Rule
  • Example 1: Primitives (Pass by Value)
  • Example 2: Objects (Pass by Value of the Reference)
  • Why People Think Java Uses Pass by Reference
  • Immutable Objects (e.g., String)
  • Common Pitfalls and Solutions
    • 1. Trying to Swap Primitive Values
    • 2. Reassigning Object References
  • Key Takeaways
  • Conclusion

Key Definitions

1. Pass by Value

  • A copy of the variable’s value is passed to the method.
  • Changes to the copy do not affect the original variable.

2. Pass by Reference

  • The memory address of the variable is passed to the method.
  • Changes to the parameter directly modify the original variable.

Java Does NOT Support Pass by Reference.

Java’s Strict Pass by Value Rule

Data TypeWhat’s Passed?Effect of Modifications
Primitives (e.g., int, double)Copy of the value.Changes inside the method stay local.
ObjectsCopy of the reference (memory address).Changes to the object’s state affect the original. Reassigning the reference does NOT affect the original.

Example 1: Primitives (Pass by Value)

Primitives pass a copy of their value. Modifying the parameter inside the method has no effect on the original.

public class Main {  
    public static void main(String[] args) {  
        int num = 10;  
        modifyPrimitive(num);  
        System.out.println(num); // Output: 10 (unchanged)  
    }  

    static void modifyPrimitive(int value) {  
        value = 20; // Only the local copy changes  
    }  
}  

Output:

The output of the above code will be: 10

Example 2: Objects (Pass by Value of the Reference)

When passing objects, a copy of the reference (memory address) is passed:

  • Modifying the object’s state (e.g., changing a field) affects the original.
  • Reassigning the reference (e.g., obj = new Object()) does not affect the original.
class Dog {  
    String name;  
    Dog(String name) { this.name = name; }  
}  

public class Main {  
    public static void main(String[] args) {  
        Dog myDog = new Dog("Radha");  
        modifyObject(myDog);  

        System.out.println(myDog.name); // Output: Krishna (state changed)  
    }  

    static void modifyObject(Dog dogRef) {  
        dogRef.name = "Krishna"; // Modifies the original object  
        dogRef = new Dog("Shyam"); // Reassigns the local copy (original unchanged)  
    }  
}  

Output:

The output of the above code will be: Krishna

Why People Think Java Uses Pass by Reference

  1. Object State Changes Are Visible:
    • If you modify an object’s fields (e.g., dogRef.name = "Krishna"), the original object reflects the change.
  2. Misunderstanding References:
    • The method receives a copy of the reference, not the original reference itself.

Immutable Objects (e.g., String)

Immutable objects behave like primitives because their state cannot change. Reassigning them inside a method has no effect outside:

public static void main(String[] args) {  
    String name = "Radha";  
    modifyString(name);  
    System.out.println(name); // Output: Radha (unchanged)  
}  

static void modifyString(String str) {  
    str = "Krishna"; // Creates a new String; original reference unchanged  
}  

Common Pitfalls and Solutions

1. Trying to Swap Primitive Values

Mistake:

void swap(int a, int b) { ... } // Useless; primitives are pass by value  

Fix: Return a result or use wrapper classes (e.g., int[]).

2. Reassigning Object References

Mistake:

void resetList(List<String> list) {  
    list = new ArrayList<>(); // Original reference outside remains unchanged  
}  

Fix: Modify the existing object (e.g., list.clear()).

Key Takeaways

  1. Java is Pass by Value: Always.
  2. Primitives: Copies of values are passed.
  3. Objects: Copies of references (memory addresses) are passed.
    • Object state changes are visible outside the method.
    • Reference reassignments are not visible.
  4. No Pass by Reference: Java does not allow direct memory manipulation.

Conclusion

Understanding pass by value in Java is critical for debugging and writing predictable code. While objects may appear to behave like pass by reference, Java’s strict pass by value rule ensures developers maintain control over data flow.

Related Posts:

  • Control Statements in Java
  • Primitive data types in Java
  • Default Variable Initialization in Java
  • Variable in Java
  • == vs .equals() in Java: What’s the Difference?
  • Arrays of Primitive Types in Java
Tags:javalanguage-fundamentals
Was this article helpful?
← Previous ArticleExecute single JUnit Test using command line in Java
Next Article →Access Modifiers in Java

Leave a Comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • How to implement Passwordless Authentication in Spring Boot: A Step-by-Step Guide
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
© 2026 CoderSathi. All rights reserved. Privacy Policy · Sitemap