Explain Call-by-value

Call-by-value: The semantics of passing an argument to a method in which a copy of actual argument value is taken and positioned in a separate memory location, symbolized by the corresponding formal argument. As an outcome, assignment to the formal argument in a method can encompass no effect on the value stored in actual argument. This principle is frequently misunderstood in Java. This does not mean that an object referred to by an actual argument can’t be modified through the formal argument. Consider the illustration of sorting the array termed to by the variable numbers:

Arrays.sort(numbers);

The sort method will modify the order of the values stored in the object referred to by numbers. Though, it is not possible for the sort method to modify that array numbers refers to - a sorted copy, for example. Some languages give an argument passing semantics termed as call-by-reference, in which an actual argument's value might be modified. Java does not offer this, however.

   Related Questions in Programming Languages

©TutorsGlobe All rights reserved 2022-2023.