Tag: pass by value

使用引用调用交换两个数字

我们可以使用传递引用或通过引用调用在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; } } 在main方法中,我调用上面提到的方法并取两个整数a , b然后使用第二种方法我交换两个数字,但相对于对象本身…. 该程序或逻辑是否通过引用传递? 这是正确的解决方案吗?

垃圾收集 – 为什么c3在此示例中不符合收集条件(SCJP 6)

摘自SCJP 6准备书 – 鉴于: class CardBoard { Short story = 200; CardBoard go(CardBoard cb) { cb = null; return cb; } public static void main(String[] args) { CardBoard c1 = new CardBoard(); CardBoard c2 = new CardBoard(); CardBoard c3 = c1.go(c2); c1 = null; // do Stuff } } 当达到// doStuff时,有多少对象符合GC条件? A. 0 B. 1 […]

请解释此Java数组参考参数传递行为

public class TestArray { public static void main(String[] args) { int[] ar = {1,2,3,4,5,6,7,8,9}; shiftRight(ar); for (int i = 0; i < ar.length; i++) { System.out.print(ar[i]); } // prints: 912345678 — good System.out.println(); reverseArray(ar); for (int i = 0; i 0; i–) { ar[i] = ar[i – 1]; } ar[0] = temp; } public static […]