Class 10 ICSE - Java

Call By Value/Call by Reference in java

Invoke Methods



When we define any method in a class,we basically tells that this method exist and inside it it tells what it will do.To invoke a method we will use this following statement in void main() static method

printmessage();
Now we will understand,how we can invoke this method in a program
Working:
This program has a class named as printHello and also has two methods.The first method printmessage() used to print the message.The second method showmessage() used to call the first method printmessage.The calling statement printmessage() calls the method first.

Call By Value



Call by value referred to passing of a paramter to a function ,then that values of actual parameter is passed to formal paramters.This means When we call a method with passing argument value to the parameters.

These value are copied to small portion of memory and copy of this argument value is passed to parameters of the called method.

This argument value is used inside the called method for Read and write operation.Since here we are using the copied argument value,so it wont affect the original value,changes will be there only on that copied value
Let us understand this concept using the following example.
In this example,when show() function starts executing,it will first intialize value of x as 10.And it will be stored in memory.
After that when the modify() function is triggered,the value of x as 10 gets copied to the formal parameters named as data.And it will also be stored in that small heap of memory.
When the statement inside function modify() is processed,only the value of formal parameters data changes to 20.It wont affect actual parameter value ,as we are using copy of that actual value.
When this function show() will get executed,you will see the following output as:

Call by Reference



In call by reference,reference denotes memory location of variable
Call by reference referred to passing of a parameter to a function as a reference.This means when we call a method by passing reference argument as a parameter.Then we are sending both arguments as actual and formal paramters.Both the parameters represent the same memory location
That means,the called function works on original data rather than copied data of actual parameter.Any changes made in that particular function,will be reflected in the actual parameter when the function is called.
Let us understand this concept using the following example.
In this example when display() function starts executing,it will first create a reference variable as ch and also D as a object.And new reference variable created as ch will refer to new object D.
After that when modify() function is triggered,new reference variable will be created and it will point to the same object D.That object reference variable will be passed to formal parameter ob.
When the statement inside modify() is executed,any changes made in the value of formal parameters get reflected in the value of actual parameter.
When this function display() will get executed,you will see the following output.

Contact Us

REACH US

SERVICES

  • CODING
  • ON-LINE PREPARATION
  • JAVA & PYTHON

ADDRESS

B-54, Krishna Bhawan, Parag Narain Road, Near Butler Palace Colony Lucknow
Contact:+ 919839520987
Email:info@alexsir.com