When a method is called by passing a value as an argument, it is referred to as call by value. With Java's call-by-value feature, a copy of the variable is passed to the method. As a result, any modifications made within the method only affect that method and do not impact the original variable in the main method.
- Java always uses call by value, even for objects.
- When a primitive type (like int, double, float) is passed to a method, a copy of the value is made.
- Changes inside the method do not affect the original variable.
Call by Reference (Object Reference)
Call by Reference means passing a reference (memory address) of the object by value to a method. Even though Java strictly uses call by value, it duplicates the reference before passing it as a value to the method when we send the reference of an object. The most important difference between call by value and call by reference in Java is that the copied reference also points to the same address, ensuring that all modifications are reflected in the main method.
- Java does not support true call by reference; it passes the reference by value.
- When an object is passed, a copy of the reference is made.
- The reference still points to the same object, so modifications inside the method affect the original object.
