SE450: Basics: Passing Parameters [32/63] Previous pageContentsNext page

What does the following print?

file:Main.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
package basics.swap;
public class Main {
  private Main() {}
  static public void main (String[] args) {
    //stdlib.Trace.graphvizShowSteps (true); stdlib.Trace.showBoxedPrimitivesAsPrimitive(false); stdlib.Trace.run ();
    Integer x = 42;
    Integer y = 27;
    Main.swap(x,y);
    System.out.println(x);
    System.out.println(y);
  }
  static private void swap (Integer a, Integer b) {
    Integer t = a;
    a = b;
    b = t;
  }
}
trace-basics-swap-004-Main_main_8 trace-basics-swap-005-Main_swap_13 trace-basics-swap-006-Main_swap_14 trace-basics-swap-007-Main_swap_15 trace-basics-swap-008-Main_swap_16 trace-basics-swap-009-Main_main_9

Java is call-by-value.

Call by Value Links

Cup Size -- a story about variables

by JavaRanch.

Pass-by-Value Please

By JavaRanch.

Does Java pass by reference or pass by value? Why can't you swap in Java?

By Tony Sintes.

Previous pageContentsNext page