Let us review the computer science terms that describe how parameters can be passed to amethod (or a function) in a programming language. The term call by value means that themethod gets just the value that the caller provides. In contrast, call by reference means thatthe method gets the location of the variable that the caller provides. Thus, a method can modifythe value stored in a variable that is passed by reference but not in one that is passed byvalue. These “call by . . . ” terms are standard computer science terminology that describethe behavior of method parameters in various programming languages, not just Java. (Infact, there is also a call by name that is mainly of historical interest, being employed in theAlgol programming language, one of the oldest high-level languages.)
The Java programming language always uses call by value. That means that the method gets a copy of all parameter values. In particular, the method cannot modify the contentsof any parameter variables that are passed to it.
For example, consider the following call:
No matter how the method is implemented, we know that after the method call, thevalue of percent is still 10.
Let us look a little more closely at this situation. Suppose a method tried to triple thevalue of a method parameter:
However, this does not work. After the method call, the value of percent is still 10. Here iswhat happens:
Modifying a numeric parameter has no lasting effect
There are, however, two kinds of method parameters:
You have seen that it is impossible for a method to change a primitive type parameter.The situation is different for object parameters. You can easily implement a method that triples the salary of an employee:then the following happens:
Modifying an object parameter has a lasting effect
As you have seen, it is easily possible—and in fact very common—to implement method sthat change the state of an object parameter. The reason is simple. The method gets acopy of the object reference, and both the original and the copy refer to the same object.
Many programming languages (in particular, C++ and Pascal) have two methods for parameter passing: call by value and call by reference. Some programmers (and unfortunately even some book authors) claim that the Java programming language uses call byreference for objects. However, that is false. Because this is such a common misunderstanding,it is worth examining a counter example in detail.
Let’s try to write a method that swaps two employee objects:
If the Java programming language used call by reference for objects, this method would work:
However, the method does not actually change the object references that are stored inthe variables a and b. The x and y parameters of the swap method are initialized with copiesof these references. The method then proceeds to swap these copies.
But ultimately, this is a wasted effort. When the method ends, the parameter variables xand y are abandoned. The original variables a and b still refer to the same objects as theydid before the method call.
Swapping object parameters has no lasting effect
This discussion demonstrates that the Java programming language does not use call byre ference for objects. Instead, object references are passed by value.
Here is a summary of what you can and cannot do with method parameters in the Java programming language:
The program in Listing below demonstrates these facts. The program first tries to triple thevalue of a number parameter and does not succeed:Testing tripleValue: Before: percent=10.0 End of method: x=30.0 After: percent=10.0
It then successfully triples the salary of an employee:Testing tripleSalary: Before: salary=50000.0 End of method: salary=150000.0 After: salary=150000.0
After the method, the state of the object to which harry refers has changed. This is possible because the method modified the state through a copy of the object reference.
Finally, the program demonstrates the failure of the swap method:Testing swap: Before: a=Alice Before: b=Bob End of method: x=Bob End of method: y=Alice After: a=Alice After: b=Bob
As you can see, the parameter variables x and y are swapped, but the variables a and bare not affected.
C++ NOTE: C++ has both call by value and call by reference. You tag reference parameters with &. For example, you can easily implement methods void tripleValue(double& x) or void swap(Employee& x, Employee& y) that modify their reference parameters.
Core Java Related Interview Questions
|J2EE Interview Questions||Core Java Interview Questions|
|JDBC Interview Questions||JSP Interview Questions|
|Android Interview Questions||JavaServer Faces (JSF) Interview Questions|
|Java collections framework Interview Questions||Java 8 Interview Questions|
|Java Collections Interview Questions||Java Exception Handling Interview Questions|
|Java Concurrency Interview Questions||Java Serialization Interview Questions|
|Java Programmer Interview Questions||Java Inheritance Interview Questions|
|Java IO Interview Questions||Object Oriented Programming in PHP Interview Questions|
Core Java Tutorial
An Introduction To Java
The Java Programming Environment
Fundamental Programming Structures In Java
Objects And Classes
Interfaces And Inner Classes
User Interface Components With Swing
Deploying Applications And Applets
Exceptions, Logging, Assertions, And Debugging
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.