(a) Exercise: Discuss the concept of \"by value\" and \"by reference\" for primi
ID: 3835232 • Letter: #
Question
(a) Exercise: Discuss the concept of "by value" and "by reference" for primitive and composite data types. Illustrate each concept with an example in Java. (a) Exercise: Consider the following scenario: At a convenience store, the worker arranged cans of drinks on the shelf one after another. The drink that was placed last could be reached easily and hence, taken out first. Conversely, the drink that was placed first would be the last to be taken out. i. Suggest a suitable abstract data structure for the scenario described above. ii. Design an ADT supporting FOUR (4) common operations of the proposed data structure. Briefly describe each operation.Explanation / Answer
Call by value:
There is only call by value in java, not call by reference. If we call a method passing a value, it is known as call by value. The changes being done in the called method, is not affected in the calling method.
class Operation{ int data=50; void change(int data){ data=data+100;//changes will be in the local variable only } public static void main(String args[]){ Operation op=new Operation(); System.out.println("before change "+op.data); op.change(500); System.out.println("after change "+op.data);} }
Call by reference :- As the name indicates, in this case we pass the memory reference or the object(also known as the instance of the class to a function. In this method the values of the actual parameters are affected with every change in the formal parameters. For example :-
class cbr
{
int a,b;
public void add(cbr obj)
{
obj.a += 10;
obj.b +=10;
}
}
class test
{
public static void main(String args[])throws IOException
{
cbr ob= new cbr(); \ creating an object of a class
ob.a=10; \ initializing a and b
ob.b=20;
System.out.print(“Values of a and b before passing an object to the function =>”);
System.out.println(ob.a);
System.out.println(ob.b);
ob.add(ob); \passing an object to the function add (call by reference)
System.out.print(“Values of a and b after passing an object to the function =>”);
System.out.println(ob.a);
System.out.println(ob.b);
}
Abstract class in Java
A class that is declared as abstract is known as abstract class. It needs to be extended and its method implemented. It cannot be instantiated.
Example abstract class
abstract method
Example abstract method
Example of abstract class that has abstract method
In this example, Bike the abstract class that contains only one abstract method run. It implementation is provided by the Honda class.
abstract class Bike
{ abstract void run(); }
class Honda4 extends Bike
{ void run(){System.out.println("running safely..");} public static void main(String args[]){ Bike obj = new Honda4(); obj.run(); } }
Call by value:
There is only call by value in java, not call by reference. If we call a method passing a value, it is known as call by value. The changes being done in the called method, is not affected in the calling method.
Example of call by value in javaRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.