• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

equals, == and boxing/widening

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

The following is a test script for == and equals() behavior. It's based on an earlier post a found, I just added some cases and a note for each explaining the behavior. I would appreciate feedback on the comments. In particular I'm not sure about b.equals(e)...

Since equals() uses instanceof, I assume that only object type matters and reference variable type is not relevant?



public class TrickyBox {

public static void main(String [] args) {

Integer a = new Integer(12);
Integer b = 12;
int c = 12;
Object d = new Long(12);
Object e = 12;
Integer f = 128;
Object g = 128;

if (a == b) System.out.println("a == b"); // false (compares Integer objects only one of which was created by autoboxing so the object was not pooled (2 different objects))
if (a == c) System.out.println("a == c"); // true (so 'a' being unboxed)
if (a == d) System.out.println("a == d"); // false (Integer being compared with an Object (2 different objects))
if (a == e) System.out.println("a == e"); // false (")
if (b == c) System.out.println("b == c"); // true (so 'b' being unboxed)
if (b == d) System.out.println("b == d"); // false (object comparison with == (not same object so not == ))
if (b == e) System.out.println("b == e"); // true (so 'b' and 'e' refer to the same object... pooling occurs for values -128 to 127 based on object type and nor reference variable type))
if (c == d) System.out.println("c == d"); // false
if (c == e) System.out.println("c == e"); // true (so 'e' being unboxed)
if (d == e) System.out.println("d == e"); // false (different objects)
if (f == g) System.out.println("f == g"); // false (objects not pooled since above 127)
if (a.equals(b)) System.out.println("a and b are equals"); // true (equal values and both are Integers))
if (a.equals(c)) System.out.println("a and c are equals"); // true (c boxed)
if (a.equals(d)) System.out.println("a and d are equals"); // false (can't widen)
if (a.equals(e)) System.out.println("a and e are equals"); // true (e boxed)
if (b.equals(c)) System.out.println("b and c are equals"); // true (c boxed to Integer)
if (b.equals(d)) System.out.println("b and d are equals"); // false (can't widen)
if (b.equals(e)) System.out.println("b and e are equals"); // true (b and e boxed??)
// compile error since c is an int and has no equals method !
// if (c.equals(d)) System.out.println("c and d are equals");
// if (c.equals(e)) System.out.println("c and e are equals");
if (d.equals(e)) System.out.println("d and e are equals"); // false (can box e to Integer but can't widen to Long))
// compile error because you can't cast an Integer as a Long (not in the same hierarchy)
// if (a.intValue() == ((Long)b).longValue()) System.out.println("EQ ab");
if (a.intValue() == ((Long)d).longValue()) System.out.println("EQ ac"); // true
if (a.intValue() == ((Long)d).longValue()) System.out.println("EQ ac"); // true
if (new Integer(2).equals(new Integer(2))) System.out.println("equals new"); // true

}
}
[ January 07, 2007: Message edited by: Paul Lachance ]
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

if (c == e) System.out.println("c == e"); // true (so 'e' being unboxed)



Boxing is a function of the wrapper classes, and Object is not a wrapper. I am guessing c == e because in both cases, the reference variable is pointing to an int literal that falls within the -128 to 127 range.
 
They gave me pumpkin ice cream. It was not pumpkin pie ice cream. Wiping my tongue on this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic