Adobe Interview Question
Computer ScientistsTeam: Big Data
Country: United States
Interview Type: In-Person
I have written in context to java , where everything is call by value , but when we pass the actual object the modifications done in the calle function makes it affect to the actual object
Even in Java, the actual object on the heap is not copied. It behaves as you say, but only because the reference to the object is being copied and passed by value. In your example, there are two variables named "or", both of which reference the same Order object. Inside your orderSubmission() function, you can use the local "or" to access the object, but you could reassign the local copy of "or" to point to a new Order object, without affecting what the calling code's "or" reference points to.
I am pasting a code where i did the same thing but it is changing the calling code reference.
{
package com.test;
public class CallByReference {
public static void main(String[] args) {
Number a = new Number();
a.x = 3;
System.out.println("Value of a.x before calling increment() is " + a.x);
increment(a);
System.out.println("Value of a.x after calling increment() is " + a.x);
}
public static void increment(Number n) {
Number temp = n;
System.out.println("Value of temp before incrementing x is " + temp.x);
temp.x = temp.x + 1;
System.out.println("Value of temp.x after incrementing x is " + temp.x);
}
}
class Number {
int x;
}
}
the output is
Value of a.x before calling increment() is 3
Value of temp before incrementing x is 3
Value of temp.x after incrementing x is 4
Value of a.x after calling increment() is 4
Yes, this is all as expected. As you can see, the Number object is not copied. There are 3 reference variables in play: main::a, increment::n and increment::temp and you are making them all point to the same Number object. It's pass by value because increment::n is a copy of main::a, that is to say, main::a is passed by value. Run the following modification to your program and you will see what I mean:
package com.test;
public class CallByReference
{
public static void main(String[] args)
{
Number a = new Number();
a.x = 3;
System.out.println("Value of a.x before calling increment() is " + a.x);
increment(a);
System.out.println("Value of a.x after calling increment() is " + a.x);
}
public static void increment(Number n)
{
System.out.println("Value of n.x before re-assign is " + n.x);
n = new Number();
n.x = 7;
System.out.println("Value of n.x after re-assign is " + n.x);
}
}
class Number {
int x;
}
Value of a.x before calling increment() is 3
Value of n.x before re-assign is 3
Value of n.x after re-assign is 7
Value of a.x after calling increment() is 3
The program changes what 'n' points to, without affecting 'a', which is possible because 'n' is a copy of 'a', not a reference to 'a'. It doesn't matter so much in Java, because there is no other way to pass the object to the function, but in other languages like C++ you have multiple options that behave differently. It's possible in C++ to actually pass 'a' by reference and change what it points to.
OK great!
For those who are familiar with Java and are scratching their heads wondering why this example is a pass-by-value situation and not pass-by-reference, it comes down to what exactly is being put on the call stack. In the above example, think of "a" and "n" as being integers. "n" is a new variable that gets a copy of the value of "a", hence pass by value. For it to be a pass-by-reference call, you would have to pass the memory address of "a" itself to the function, so that even what "a" points to could be altered. This one level of indirection is the distinction. You cannot do this in Java, but that is what is meant by reference in other languages.
In call by value a copy of variable is made and all the computation is done on the copied variable not affecting the original value .like if we have int x=9 , int y=8; and we want to pass this to add function
- akshaymattoo November 01, 2014int add(int u,int v){} .... then the copies of x and y are made into u and v respectively , without affecting the values of x and y .
In contrast in call by reference the actual object is being copied and the computation being done in the method affects the actual object.
like we have a order object .
Order or = new Order();
or.setName("Adobe");
now we have method pass this order object to a method
public int orderSubmission(Order or){} and set the name of order different as Google , Now the original value that was Amazon gets changed .