views:

576

answers:

5

When I try to declare a Dictionary as such:

private Dictionary<String, int> map;

The compiler gives me the following error:

Syntax error on token "int", Dimensions expected after this token

But it works fine with Integer. I'm vaguely aware that java treats int / Integer differently (I come from a .NET background), but I was hoping someone could give me a full explanation on I can't use primitives in a Dictionary<>

+1  A: 

Java collections only allow references not primitives. You need to use the wrapper classes (in this case java.lang.Integer) to do what you are after:

private Dictionary<String, Integer> map;

they you can do things like:

int foo = map.get("hello");

and

map.put("world", 42);

and Java uses autoboxing/unboxing to deal with the details of the conversion for you.

Here is a little description on it.

TofuBeer
I mentioned that it works `Integer` in my original post. I was hoping for an explanation on why
Richard Szalay
Thanks for the edit, I've removed the -1 :)
Richard Szalay
Isn't int[] not a primitive? It might just be expecting an array, right?
Juan Besa
Technically, `Dictionary` isn't part of the Java Collections framework, so this doesn't really explain why `int` is disallowed in `Dictionary` objects.
delfuego
Arrays are references not primitives: "A variable of array type holds a reference to an object." http://java.sun.com/docs/books/jls/third_edition/html/arrays.html#10.2
TofuBeer
So an int[] would work?
Juan Besa
Dictionary has existed in Java for a very long time, before interfaces were added (which is why it isn't an interface but it should be). The collections framework was added in JDK 1.2 to address the lack of common collections. So it is not part of the framework, but it is a distant relative :-) There is no way in Java to have a variable that holds both a primitive and a reference so there is no way to make a Dictionary that works for primitives and references at the same time. You would have to make an IntDictionary that worked on ints...
TofuBeer
Yes, int[] works just fine: final Dictionary<String, int[]> foo; foo = new Hashtable<String, int[]>(); foo.put("a", new int[] {1, 2, 3}); System.out.println(Arrays.toString(foo.get("a")));
TofuBeer
Ok :) thanks! TofuBeer
Juan Besa
TofuBeer, that's all true; I was just pointing out that using Java collections as an explanation didn't really explain, since `Dictionary` objects aren't collections in the contemporary Java meaning of the term. Your latter explanation is the real reason.
delfuego
I lump dictionary in with the collections as well... they are just the "poor mans" version :-)
TofuBeer
+4  A: 

In Java primitives aren't objects, so you can't use them in place of objects. However Java will automatically box/unbox primitives (aka autoboxing) into objects so you can do things like:

List<Integer> intList = new LinkedList<Integer>();
intList.add(1);
intList.add(new Integer(2));
...
Integer first = intList.get(0);
int second = intList.get(1);

But this is really just the compiler automatically converting types for you.

Jeremy Raymond
A: 

Because in Java the primitives are truely primitives. In Java int will pass by value, while Integer will pass a reference. In .NET int or Int32 etc. are just different names.

Y. Shoham
Except generics are part of the compilation process- nothing is ever "passed" in the poster's example.
JohnE
FYI, int/Int32 will pass by reference in .NET too since they are value types (and thus live on the stack).
Richard Szalay
Also, `int` is only an alias for `Int32` in c#. VB.NET uses `Integer`
Richard Szalay
+2  A: 

In .Net, "primitive" types are backed by objects. In Java, there's a hard distinction between primitive types and Objects. Java 5 introduced autoboxing, which can coerce between the two in certain situations. However, because the Java generics system uses type-erasure, there isn't enough information to autobox in this case.

JohnE
A: 

To expand on TofuBeer's answer.

int is a primitive

Integer is an Object.

Generics does not support primitives.

Knife-Action-Jesus
I'd say that's more of a summary than an expansion :)
Richard Szalay
He edited his post since mine :)
Knife-Action-Jesus