+ 2
What is the wrong in this code
3 odpowiedzi
+ 1
class Swapper{
int a;
int b;
Swapper(int x,int y){
a = x;
b = y;
}
void swap(int a,int b){
int temp;
temp = a;
a = b;
b = temp;
}
}
class SwapDemo{
public static void main(String args[]){
Swapper obj = new Swapper(10,20);
System.out.println("before"+obj.a+obj.b);
obj.swap(obj.a,obj.b);
System.out.println("after"+obj.a+obj.b);
}
}
you cannot use star next to variables like in c or c++ for pointers. i removed the star characters and the code runs fine.
+ 1
But this code is call by value but I want to pass reference to variable
0
in java you can only pass parameters by value, as well as object references:
https://www.google.com/amp/s/www.javaworld.com/article/3512039/does-java-pass-by-reference-or-pass-by-value.amp.html