when I declare a Map or Map <Object,Object> I can put anything in this Map
Map map = new HashMap(); map.put(""); but if I declare it as Map <?,?> I can put nothing in it
Map<?,?> map = new HashMap(); map.put(""); it will goes wrong why ?
when I declare a Map or Map <Object,Object> I can put anything in this Map
Map map = new HashMap(); map.put(""); but if I declare it as Map <?,?> I can put nothing in it
Map<?,?> map = new HashMap(); map.put(""); it will goes wrong why ?
Map<?,?> map = new HashMap<Integer, Integer>(); // compiles just fine! ? represents some fixed but unknown type. You can't put "" in a Map<Integer, Integer>, and a Map<?, ?> is allowed to be any type of Map, including a Map<Integer, Integer>.
For this snippet code:
Map<?,?> map = new HashMap(); Map<?,?> means a Map typed to an unknown type.
The question mark (?), called the wildcard,the wildcard means "the value type parameter could be anything", it doesn't mean "you can use this as if it were anything you want it to be".
For more info go to link