8

This is from Professor Mark Weiss in his book Data Structures and Algorithm Analysis in Java

public class BinaryHeap<AnyType extends Comparable<? super AnyType>>{
    private void enlargeArray( int newSize ){
        AnyType [] old = array;
        array = (AnyType []) new Comparable[ newSize ];
        for( int i = 0; i < old.length; i++ )
        array[ i ] = old[ i ];        
    }
}

I was wondering why do we declare an array with a type of interface Comparable since we have to convert the Comparable[] to an AnyType[]? Any design philosophy in there?

16
  • 3
    Because you can't create an array of type AnyType[]. Commented Apr 14, 2017 at 16:59
  • 1
    @user2357112 it's not that wrong. The compiler will generate an unchecked cast warning, but you can't really do anything better when you have to store objects of a generic type in an array. As long as the array is private and isn't exposed, this is fine. Commented Apr 14, 2017 at 17:03
  • 1
    @LewBloch they haven't :-) Commented Apr 14, 2017 at 17:05
  • 1
    @JBNizet: Yeah, you can't get away from the unchecked cast without an explicit Class object, but it's still better to perform the unchecked casts on the elements (which are actually expected to have the type you're casting them to) than the array (which definitely isn't of the type you'd be casting it to). Commented Apr 14, 2017 at 17:09
  • 1
    @user2357112 that will work without any problem though, and at least it will cause the compiler to complain if you try storing something other than an AnyType in the array. That won't be the case if you use a Comparable[]. BTW, the class com.sun.jmx.remote.internal.ArrayQueue does use that. Commented Apr 14, 2017 at 17:16

2 Answers 2

4

The design "philosophy" is that you can't instantiate an array of a type parameter, so you have to instantiate the array with a type that is legal. The only available legal types known to the method are array of Object or of Comparable, and the latter captures more knowledge about the type.

You are allowed to downcast to an array of the type parameter, and the return type has to be that, so downcasting is required.

It's the "philosophy" of necessity.

Sign up to request clarification or add additional context in comments.

2 Comments

"You are allowed to downcast to an array of the type parameter" - you're only "allowed" in the sense that these casts are unchecked and Java doesn't have the information it'd need to stop you.
You're allowed to in the sense that the program compiles and runs, in other words.
0

AnyType I believe is a generic. In Java you can't create an array of generics. As a workaround for this, he has instantiated an array of Comparables (an interface) and then type-casted it to an array of generics.

If you look at this "how to create an array of generics" question, one person offers initializing an array of Objects and then type-casting to an array of the desired generic.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.