Dave Durbin
Dave Durbin

Reputation: 3632

Can you explain odd behaviour of Boxing and Generics in Java

According to the Java docs the following code ought to cause a compile error:

import java.util.*;

public class GenericTest1 {

    // Add T-array of objects to collection<T>
    static <T> void fromArrayToCollection(T[] a, Collection<T> c) {
        for (T o : a) {
            c.add(o);
        }
    }

   public static void main( String[] args ) {
       Number[] na = new Number[100];
       Collection<Number> cn = new ArrayList<Number>();


       // This should work and does
       fromArrayToCollection( na, cn );


       Collection<String> cs = new ArrayList<String>();

       // This should fail to copile and does
       fromArrayToCollection( na, cs );
   }
}

and it does:

GenericTest1.java:25: error: method fromArrayToCollection in class GenericTest1 cannot be applied to given types;
       fromArrayToCollection( na, cs );
       ^
  required: T[],Collection<T>
  found: Number[],Collection<String>
  reason: inference variable T has incompatible bounds
    equality constraints: String
    lower bounds: Number
  where T is a type-variable:
    T extends Object declared in method <T>fromArrayToCollection(T[],Collection<T>)

However, this compiles and runs perfectly.

public class GenericTest2 {

    // Test for equality of two objects of type T
    static <T> boolean testEquality(T first, T second ) {
        return first.equals( second );
    }


   public static void main( String[] args ) {
       // Should work
       System.out.println( testEquality( "One", "One" ) );

       // Shouldn't this refuse to compile ?
       System.out.println( testEquality( "One", 1 ) );

       // Shouldn't this refuse to compile ?
       Number one = new Integer( 1 );
       System.out.println( testEquality( "One", one ) );

   }
}

Output is :

true
false
false

Can anyone explain why ?

Upvotes: 1

Views: 70

Answers (3)

tning
tning

Reputation: 1251

testEquality( "One", 1 )

As a result of autoboxing, 1 here will be converted to Integer(1), which is an object. Both String("One") and Integer(1) inherited .equals function from Object, thus it can compile without error.

Upvotes: 1

LionC
LionC

Reputation: 3106

It works because one(Number) and "One"(String) are both Objects, as are 1(Integer due to autoboxing) and "One"(String). So T is evaluated to Object, equals gets called and returns false. It does not work with the Collection(and other generics for that matter) because a Collection<String> can not be cast to a Collection<Object>

Upvotes: 2

Mirko Stocker
Mirko Stocker

Reputation: 2242

In your second test, the 1 will simply be boxed into an Integer instance, which is also an instance of Object.

Upvotes: 0

Related Questions