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
1 Réponse
+ 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;
}