Skip to content

Instantly share code, notes, and snippets.

@anandrajneesh
Last active October 17, 2016 12:29
Show Gist options
  • Save anandrajneesh/2b8f983df2a16123181b66cb140d460b to your computer and use it in GitHub Desktop.
Save anandrajneesh/2b8f983df2a16123181b66cb140d460b to your computer and use it in GitHub Desktop.

Q. How does auto-boxing handle the following code fragment?

Integer a = null;
int b = a;

A. It results in a run-time error. Primitive type can store every value of their corresponding wrapper type except null.

Q. Why does the first group of statements print true, but the second false?

Integer a1 = 100;
Integer a2 = 100;
System.out.println(a1 == a2);   // true

Integer b1 = new Integer(100);
Integer b2 = new Integer(100);
System.out.println(b1 == b2);   // false

Integer c1 = 150;
Integer c2 = 150;
System.out.println(c1 == c2);   // false

A. The second prints false because b1 and b2 are references to different Integer objects. The first and third code fragments rely on autoboxing. Surprisingly the first prints true because values between -128 and 127 appear to refer to the same immutable Integer objects (Java's implementation of valueOf() retrieves a cached values if the integer is between -128 and 127), while Java constructs new objects for each integer outside this range.

/******************************************************************************

  • Compilation: javac Autoboxing.java
  • Execution: java Autoboxing
  • Dependencies: StdOut.java
  • Headaches with autoboxing. Why does the following code behave
  • the way it does?
  • % java Autoboxing
  • 42 and 42 are incomparable
  • 43 == 43
  • 142 and 142 are incomparable
  • Explanation:
    • cmp(new Integer(42), new Integer(42))
  •  first and second refer to different objects holding the value 42;
    
  •  thus, first != second.
    
  •  The expressions (first < second) and (first > second) autounbox
    
  •  the Integer values to 42, so neither expression is true.
    
    • cmp(43, 43)
  • the values 43 are autoboxed to the same Integer object because
    
  • Java's Integer implementation caches the objects associated with
    
  • the values -128 to 127 and the valueOf() method uses the cached
    
  • values (and valueOf() gets called by the autoboxing).
    
    • cmp(142, 142)
  • the values 142 are autoboxed to different Integer objects.
    

******************************************************************************/

public class Autoboxing {

public static void cmp(Integer first, Integer second) {
    if (first < second)
        StdOut.printf("%d < %d\n", first, second);
    else if (first == second)
        StdOut.printf("%d == %d\n", first, second);
    else if (first > second)
        StdOut.printf("%d > %d\n", first, second);
    else
        StdOut.printf("%d and %d are incomparable\n", first, second);
}

public static void main(String[] args) {
    cmp(new Integer(42), 43);
    cmp(new Integer(42), new Integer(42));
    cmp(43, 43);
    cmp(142, 142);

    double x1 = 0.0, y1 = -0.0;
    Double a1 = x1, b1 = y1;
    StdOut.println(x1 == y1);
    StdOut.println(a1.equals(b1));

    double x2 = 0.0/0.0, y2 = 0.0/0.0;
    Double a2 = x2, b2 = y2;
    StdOut.println(x2 != y2);
    StdOut.println(!a2.equals(b2));
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment