Passing Arguments in Java
In Java, arguments can be passed to methods or constructors to provide data that the method or constructor will use. Java supports two ways to pass arguments:
- Pass by Value
- Pass by Reference
However, technically, Java only supports pass by value. Even objects are passed by the value of their references.
1. Pass by Value
When you pass arguments to a method in Java, a copy of the value is passed. Modifying the parameter inside the method does not change the original value.
Example: Pass by Value with Primitive Types
class Test {
void modifyValue(int num) {
num = num + 10; // This modifies the local copy
System.out.println(“Inside method: ” + num);
}
}
public class Main {
public static void main(String[] args) {
int num = 5;
Test test = new Test();
test.modifyValue(num); // Passing num by value
System.out.println(“After method call: ” + num); // Original value remains unchanged
}
}
Output:
Inside method: 15
After method call: 5
Here, the original value of num in main remains unchanged.
2. Passing Objects (Pass by Value of Reference)
When passing an object to a method, a copy of the reference is passed, not the actual object. This means changes to the object’s fields will affect the original object, but reassigning the reference won’t affect the original.
Example: Modifying Object Properties
class Person {
String name;
void changeName(String newName) {
name = newName; // Changes the object property
}
}
public class Main {
public static void main(String[] args) {
Person person = new Person();
person.name = “John”;
person.changeName(“Alice”); // The method modifies the object
System.out.println(“Name after method call: ” + person.name); // Reflects change
}
}
Output:
Name after method call: Alice
Here, the object’s property is updated because the reference points to the same object in memory.
Example: Reassigning the Reference
class Person {
String name;
}
class Test {
void changeReference(Person person) {
person = new Person(); // Creates a new object
person.name = “Alice”; // Modifies the new object
}
}
public class Main {
public static void main(String[] args) {
Person person = new Person();
person.name = “John”;
Test test = new Test();
test.changeReference(person); // The reference change is local to the method
System.out.println(“Name after method call: ” + person.name); // Original object remains unchanged
}
}
Output:
Name after method call: John
Here, the method’s reassignment of the reference does not affect the original reference in the calling method.
Key Concepts:
- Primitive Types:
- Passed by value.
- Any modification within the method does not affect the original variable.
- Objects:
- A copy of the object’s reference is passed.
- Modifications to the object’s fields will affect the original object.
- Reassigning the reference inside the method does not change the original reference.
Passing Arguments to main Method
The main method in Java takes an array of String arguments as input. This allows you to pass command-line arguments to a program.
Example: Passing Command-Line Arguments
public class Main {
public static void main(String[] args) {
for (String arg : args) {
System.out.println(arg);
}
}
}
Run Command:
java Main Hello World
Output:
Hello
World
Conclusion
- Java always passes arguments by value. For primitives, it’s the value itself; for objects, it’s the value of the reference.
- Understanding how Java handles argument passing helps write clear, predictable methods and avoid unintended side effects.