34

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 why I can't use primitives in a Dictionary<>

Code Jockey
  • 6,611
  • 6
  • 33
  • 45
Richard Szalay
  • 83,269
  • 19
  • 178
  • 237

6 Answers6

41

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
  • 5,817
  • 3
  • 31
  • 33
  • 1
    Hmm, so is Java less efficient than .NET here because it forces you to box the integer before adding it to the collection? That's one of the often-stated benefits of `Dictionary` over `HashTable` in .NET -- you avoid boxing. – Tim Goodman Sep 20 '13 at 13:35
  • What is meant by "Dimensions expected after this token"? Is it referring to an array, like `int[]` which would be okay because it's an `Array` object? – ADTC Feb 11 '15 at 08:14
9

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
  • 349
  • 1
  • 3
5

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
  • 60,850
  • 18
  • 118
  • 163
  • I mentioned that it works `Integer` in my original post. I was hoping for an explanation on why – Richard Szalay Jan 04 '10 at 19:55
  • Isn't int[] not a primitive? It might just be expecting an array, right? – Juan Besa Jan 04 '10 at 19:59
  • Technically, `Dictionary` isn't part of the Java Collections framework, so this doesn't really explain why `int` is disallowed in `Dictionary` objects. – delfuego Jan 04 '10 at 20:00
  • 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 Jan 04 '10 at 20:03
  • 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 Jan 04 '10 at 20:06
  • Yes, int[] works just fine: final Dictionary foo; foo = new Hashtable(); foo.put("a", new int[] {1, 2, 3}); System.out.println(Arrays.toString(foo.get("a"))); – TofuBeer Jan 04 '10 at 20:09
  • 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 Jan 04 '10 at 20:14
  • I lump dictionary in with the collections as well... they are just the "poor mans" version :-) – TofuBeer Jan 04 '10 at 20:28
1

To expand on TofuBeer's answer.

int is a primitive

Integer is an Object.

Generics does not support primitives.

Keibosh
  • 1,237
  • 1
  • 9
  • 18
1
@XmlJavaTypeAdapter(value=MyAdapter.class, type=int.class)

Thats the trick specify type to make it work with primitives

In your adapter

using the same in package-info will mean you do it globally for that package

Found this after experimenting.

public class MyAdapter extends XmlAdapter<String, Integer> {
Abs
  • 3,902
  • 1
  • 31
  • 30
0

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.

Yaakov Shoham
  • 10,182
  • 7
  • 37
  • 45