Tuesday, November 20, 2012

Autoboxing calls Integer.valueOf(...)

Autoboxing calls Integer.valueOf(...) - see sample snippet of code:

package pl.jeeprojectsnippets.autoboxing;
public class JeeProjectAutoboxing {

    public static void main(String[] args) {
        int d [][] = {{1,2},{3,4}};
        int c [] = d[1];
        int b = c[1];
        Object f = 1; // Autoboxing "Object f = 1;" calls "Integer.valueOf(1)"
        Object a = b;
        int g = (Integer) a;
        Integer i = Integer.valueOf(1);
        Integer j = Integer.valueOf(1);
        Integer k = new Integer(1);
        Integer l = new Integer("1");
        System.out.println(g);  // toggle a breakpoint here and see id of all numbers in debug mode
    }
}

No comments: