Java Programming Handbook

    Parameter Passing in Java

    In Java, we often pass values to methods so they can perform operations using those values. This is called parameter passing. Java supports two types of parameter passing:

    • Pass-by-Value (Primitive Data Types)
    • Pass-by-Reference (Objects & Arrays)

    In this blog, we will cover:

    • How parameters work in Java
    • Passing primitive data types to methods
    • Passing objects to methods
    • Passing arrays to methods
    • Key differences between pass-by-value and pass-by-reference

    1. How Parameters Work in Java?#

    In Java, all method arguments are passed by value, meaning a copy of the value is passed to the method. However, how this affects the original variable depends on whether we pass:

    • Primitive data types (like int, double) → Original value does not change
    • Objects & arraysOriginal object can be modified

    2. Passing Primitive Data Types (Pass-by-Value)#

    When we pass a primitive data type (like int, double, char) to a method, only a copy of the value is passed. Changes inside the method do not affect the original value.

    Example: Passing Primitive Data Types#

    class Example { // Method that tries to modify the value static void modifyValue(int num) { num = num + 10; // Change the value System.out.println("Inside Method: " + num); } public static void main(String[] args) { int number = 50; // Passing primitive type to method modifyValue(number); // Original value remains unchanged System.out.println("Outside Method: " + number); } }

    Output:#

    Inside Method: 60 Outside Method: 50

    Explanation:

    • Inside the method, num is changed, but the original number remains unchanged because only a copy was passed.

    3. Passing Objects (Pass-by-Reference)#

    When we pass an object to a method, a copy of the reference (memory address) is passed. This means changes made inside the method will affect the original object.

    Example: Passing Objects to Methods#

    class Person { String name; // Constructor Person(String name) { this.name = name; } // Method that modifies the object static void changeName(Person p) { p.name = "Updated Name"; // Modifying object } public static void main(String[] args) { // Creating an object Person person1 = new Person("John"); // Printing before modification System.out.println("Before: " + person1.name); // Passing object to method changeName(person1); // Printing after modification System.out.println("After: " + person1.name); } }

    Output:#

    Before: John After: Updated Name

    Explanation:

    • The Person object is passed to the method, and its name property is changed.
    • Since objects are passed by reference, the original object is modified.

    4. Passing Arrays to Methods#

    Like objects, arrays are also passed by reference, meaning changes inside the method will affect the original array.

    Example: Passing an Array to a Method#

    class Example { // Method that modifies an array static void modifyArray(int[] arr) { arr[0] = 100; // Changing the first element } public static void main(String[] args) { int[] numbers = {1, 2, 3, 4}; // Printing before modification System.out.println("Before: " + numbers[0]); // Passing array to method modifyArray(numbers); // Printing after modification System.out.println("After: " + numbers[0]); } }

    Output:#

    Before: 1 After: 100

    Explanation:

    • The modifyArray() method changes the first element of the array.
    • Since arrays are passed by reference, the original array is modified.

    5. Key Differences: Pass-by-Value vs Pass-by-Reference#

    FeaturePrimitive Data Types (int, double, char)Objects & Arrays
    Pass TypePass-by-Value (Copy is Passed)Pass-by-Reference (Reference is Passed)
    Modification inside Method?No (Original value remains same)Yes (Original object is modified)
    Effect on Original DataNo effectChanges are reflected

    6. Returning Values from a Method#

    A method can also return modified data.

    Example: Returning a Modified Value#

    class Example { // Method that returns a new value static int addTen(int num) { return num + 10; } public static void main(String[] args) { int number = 50; // Storing the returned value number = addTen(number); System.out.println("Updated Value: " + number); } }

    Output:#

    Updated Value: 60

    Explanation:

    • The method addTen() returns a new value, and we store it in number.

    Conclusion#

    In this blog, we learned:

    • Java passes all parameters by value
    • Primitive types are passed by value (original value remains unchanged)
    • Objects & Arrays are passed by reference (original object is modified)
    • Modifications inside methods can affect objects but not primitive types

    Understanding parameter passing is important for writing efficient and bug-free Java programs.

    Last updated on Apr 09, 2025