我们可以使用传递引用或通过引用调用在Java中交换两个数字吗?最近,当我遇到用Java交换两个数字时,我写道
class Swap{
int a,b;
void getNos(){
System.out.println("input nos");
a = scan.nextInt();
b = scan.nextInt(); // where scan is object of scanner class
}
void swap(){
int temp;
temp = this.a;
this.a = thisb;
this.b = this.a;
}
}
Run Code Online (Sandbox Code Playgroud)
在main方法中我调用上面提到的方法并取两个整数a,b然后使用第二个方法我交换两个数字,但相对于对象本身....
该程序或逻辑是否通过引用传递?这是正确的解决方案吗?