Question about array of parameterized class

(Yes, another one, sorry.)

I want to construct an instance for this array:

Map<Foo,Bar> mapArray[];

This works:

mapArray = (HashMap<Foo,Bar>[])new Map[size];

but these don't work (Eclipse tells me "the cast is checking against the erased type HashMap[]"):

mapArray = (Map<Foo,Bar>[])new Map[size];

mapArray = (HashMap<Foo,Bar>[])new HashMap[size];

I'd just like to know: Why can I cast an array of an abstract type (Map) to a concrete Type (HashMap), but not cast Map to Map, or HashMap to HashMap?

This is totally counter-intuitive. It seems like it should be exactly the other way round.

[775 byte] By [ulrich-hobelmanna] at [2007-11-26 18:53:42]
# 1

I created an example file and compiled it from command line:import java.util.Map;

import java.util.HashMap;

class Test{

void test(){

int size = 10;

Map<Foo,Bar> mapArray[];

mapArray = (HashMap<Foo,Bar>[])new Map[size];

}

class Foo{}

class Bar{}

}

javac Test.java

The Test class was compiled without any errors.

I use java version "1.6.0-rc"

Which version of jdk do you use?

raindropa at 2007-7-9 6:27:43 > top of Java-index,Core,Core APIs...
# 2

I am sorry. This one also compiled without any errors:import java.util.Map;

import java.util.HashMap;

class Test{

void test(){

int size = 10;

Map<Foo,Bar> mapArray[];

mapArray = (HashMap<Foo,Bar>[])new Map[size];

mapArray = (Map<Foo,Bar>[])new Map[size];

mapArray = (HashMap<Foo,Bar>[])new HashMap[size];

}

class Foo{}

class Bar{}

}

raindropa at 2007-7-9 6:27:43 > top of Java-index,Core,Core APIs...
# 3

I'm using JDK 6, but I think Eclipse uses its own compiler.

Anyway, actually the code I posted did NOT work. It compiled, but at runtime Java couldn't cast Map to HashMap - just the way it should be.

Actually, to make it work I had to use the

(HashMap<Foo,Bar>[])new HashMap[size]

form, but Eclipse made be annotate the method "unchecked", in order to not get that stupid warning (that serves no purpose as far as I can see).

By the way: why do I have to annotate the whole method in Java? Now it might happen that by adding some code to the method I introduce something that's worth warning about, but because of the annotation I don't get the warning. Bad Java. No dessert. ;-)

ulrich-hobelmanna at 2007-7-9 6:27:43 > top of Java-index,Core,Core APIs...
# 4

First, you can annotate a local variable

public static void main(String[] args) {

@SuppressWarnings("unchecked") Map<String,String>[] array=

(Map<String,String>[])new HashMap<?,?>[3];

}

else i've recently discussed with Alex Buckley and Peter ah?br>on a way to modify the JLS in order to allow array of parametrized type creation.

i hope it will be integrated in jdk7.

R閙i

remi_foraxa at 2007-7-9 6:27:43 > top of Java-index,Core,Core APIs...
# 5
Cool, thanks!It's really great to see how after those years of slooow progress Java is really packing a punch these days! Love Java 6, looking forward to 7.
ulrich-hobelmanna at 2007-7-9 6:27:43 > top of Java-index,Core,Core APIs...