Yahoo Interview Question
Software Engineer / Developers1) for basic data type for example say... Sum(a,b) or Sum(1,2) , this is call by value , as even if the data passed to method is altered value of a , b doesn't change their values.
2) Passing array of int ( i.e primitive data type ) also belongs to call by value.
3) Whenever we pass the instance of non-primitive data type then it is pass by reference. and Hence if the value of the object gets modified in called method it would reflect in caller too.
Sorry for above comments....some correction...
When we pass variables , the copy of the variable is made in target method. Hence we will not see any change to original variables.
When we pass object... first of all we are not passing the object, we always pass reference of the object, and the reference is passed by value . What it means is There will two reference created for same object. One in target method and other is source.
Hence if you assign anything directly to target method references then source reference will continue to point to original object. But if you change change any member variable using target reference, source reference object values will also change, as both the reference are pointing to the same object.
array of int is pass-by-value-of-reference.... google it out.
Now the details...
The problem we're facing here is statements like
In Java, Objects are passed by reference, and primitives are passed by value.
This is half incorrect. Everyone can easily agree that primitives are passed by value; there's no such thing in Java as a pointer/reference to a primitive.
However, Objects are not passed by reference. A correct statement would be Object references are passed by value.
This may seem like splitting hairs, bit it is far from it. There is a world of difference in meaning. The following examples should help make the distinction.
refer : httx://javadude.com/articles/passbyvalue.htm
array of int is pass-by-value-of-reference.... google it out.
Now the details...
The problem we're facing here is statements like
In Java, Objects are passed by reference, and primitives are passed by value.
This is half incorrect. Everyone can easily agree that primitives are passed by value; there's no such thing in Java as a pointer/reference to a primitive.
However, Objects are not passed by reference. A correct statement would be Object references are passed by value.
This may seem like splitting hairs, bit it is far from it. There is a world of difference in meaning. The following examples should help make the distinction.
refer : javadude.com/articles/passbyvalue.htm
Was this question asked in Yahoo ? At which level ?
- Time pass ! February 19, 2009