There are two ways to call a method or we can say there are two ways to passing input parameters via a method. Call by value and Call by reference.
However, I have done research on the web and found that JAVA only Supports Call by Value and not Call by Reference. But I think that logically we have both the methods available in JAVA.
Call By Value or Passing Value via Method
First, we understand the Call by Value process or Passing Value process, both are same. In this process we pass only values via method parameters and all the changes have been done only in local variables within the called method without affecting original variable values.
In the above example, we can see that value of a has been passed via method changeValue(a). Method changeValue(int x), assigns 15 to x but the value of a doesn’t get changed in this process.
Related Links:
- Basic Java – 1 || Understand Java before start learning JAVA.
- Basic Java – 2 || Variables and Data Types used in JAVA.
- Basic Java – 3 || Understanding Class, Objects, Methods in Java.
- Basic Java – 4 || More on methods(Return Type and Parameters)
- Basic Java – 5 || Methods- Call by Value and Call by Reference in Java.
- Basic Java – 6 || Understanding of Constructor and Destructor in JAVA.
- Basic Java – 7 || Static Variables and Methods.
- Basic Java – 8 || Lets learn about Arrays in Java.
- Basic Java – 9 || Performing multiple operations using Java Operators.
- Basic Java – 10 || Conditions (If and Switch) in JAVA.
- Basic Java – 11 || for and for-each in Java. (Loops Part-1)
- Basic Java – 12 || Alternate looping concepts while and do-while. (Loops Part-2)
- Basic Java – 13 || Decimal values v/s Octal base(8) values in JAVA.
- Basic Java – 14 || Learn about String literals in Java.
- Basic Java – 15 || Runtime User Input using Scanner Class (Part-1).
- Basic Java – 16 || Runtime User Input using BufferedReader Class (Part-2).
- Basic Java – 17 || Runtime User Input using Console Class (Part-3).
- Basic Java – 18 || Difference between break and continue keywords.
- Basic Java – 19 || Sending Email using Java (Part-1).
- Basic Java – 20 || Sending Email with attachment using Java (Part-2).
- Basic Java – 21 || Stack memory and Heap memory in Java.
- Basic Java – 22 || Let’s learn more about String.
- Basic Java – 23 || String, StringBuffer & StringBuilder in Java.
- Basic Java – 24 || Exception Handling using Try Catch.
- QnA || How to use Constructors in Abstract class?
- QnA | Difference between Integer and int keywords.
- QnA | Can main method be overloaded?
- QnA | How do I reverse a String/Sentence in Java?
- QnA | Perform Multiplication and Division without * or / or % operators.
- QnA | How do I get the default value of data type?
- QnA | How to split String if it contains period symbol (.) in between?
Code Example:
public class CallByValueExample {
public static void main(String[] args)
{
int a=10;
System.out.println("Before Change value of a: "+a);
// Passing value as a parameter. OR
// Call method by value.
changeValue(a);
// Original value of variable ‘a’ doesn’t get changed.
System.out.println("After Change value of a: "+a);
}
public static void changeValue(int x)
{
x+=20;
System.out.println("Value of x: "+x);
}
}
Output:
Before Change value of a: 10
Value of x: 30
After Change value of a: 10
Call by Reference or Passing Reference via Method
This can be done only using the Objects. As the name depicts we have to pass a reference(address) of the objects. Actually, it works same like pointers . We pass the reference via a method which points to a variable(s), now this variable can be handled by calling method.
Example-1
public class callByRef
{
int testData=50;
public static void main(String[] args)
{
// Creating object of Class
callByRef objTemp=new callByRef();
System.out.println("Before calling method: "+objTemp.testData);
// Passing object reference in Method parameter. OR
// Calling method by reference.
objTemp.increaseValue(objTemp);
System.out.println("After calling method: "+objTemp.testData);
objTemp.testData+=25;
System.out.println("After increasing value: "+objTemp.testData);
}
public static void increaseValue(callByRef obj)
{
// Value get increased in the same variable.
obj.testData+=100;
}
}
Output:
Before calling method: 50
After calling method: 150
After increasing value: 175
Example -2
public class CallByRef_2 {
public static void main(String[] args)
{
StringBuffer sb=new StringBuffer("JAI ");
System.out.println("Before change: "+sb);
changeValue(sb);
System.out.println("After change: "+sb);
sb.append(" BHARAT");
System.out.println("After more change: "+sb);
}
public static void changeValue(StringBuffer obj)
{
obj.append("HIND");
}
}
Output:
Before change: JAI
After change: JAI HIND
After more change: JAI HIND BHARAT