0

Pass by value

what is the difference between pass by value and pass by reference.? and in java only pass by value use or both and why?? Tell me pls

9th Nov 2016, 4:02 AM
Rahul
Rahul - avatar
1 Answer
+ 1
if you pass a variable by value, a copy of it will be made so the original value won't change but when you pass a value by reference you are sending the address(reference) of your variable, therefore operations in the function will directly influence the original variable. passing by reference is present in Java but since Java doesn't have pointers you have to use the class of the variable when you are initializing it.(It's called boxing in Java) exp: public static void main(){ int a = 5; double(a); system.out.println(a); //outputs 5 } public static void double(int n){ n = 2 * n; } //pass by refrence in java public static void main(){ Integer a = 5; double(a); system.out.println(a); //outputs 10 } public static void double(int n){ n = 2 * n; }
9th Nov 2016, 4:56 AM
Hamidreza Bayat
Hamidreza Bayat - avatar