0

Why doesn't the last line of following code compile in Java?

HashMap<String, Integer> map = new HashMap();
map.put("a", 1);
map.get("a") += 1;

Something like that works fine for C++.

Rudi Kershaw
  • 12,332
  • 7
  • 52
  • 77
hovnatan
  • 1,331
  • 10
  • 23

3 Answers3

3

You are trying to change the map value by using the get method. In your code, the map.get("a"); will only RETURN a Integer. You cannot change the value of the Integer stored inside the HashMap object without using the setter method .put() Something like this is acceptable:

map.put("a",1);
map.put("a",map.get("a") + 1);

However, this:

map.get("a") += 1;

Is not acceptable and will not compile, because you cannot change the value of the object through a getter method.

The reason why getter and setter methods, like map.put() & map.get(), are so important in Java can be found here

Community
  • 1
  • 1
Turtle
  • 1,369
  • 1
  • 17
  • 32
  • I understand that I can change the value in the map by the method you mentioned. But I was interested what Java language constraint actually prohibits this kind of expressions. I think the answer is that `map.get()` returns an `Integer` which is immutable. – hovnatan Sep 10 '15 at 07:55
  • The constraint is mentioned above. You cannot change the value by use of a getter method. The only way to actually changed the value in the map is by using the setter method because you can only access the Integer object stored in the map by predefined methods. The java compiler assumes that you are trying to change the value of the Integer object and store that new value inside the map using the same map.get statement, which is why you are getting a compiler error – Turtle Sep 10 '15 at 13:17
0

Something like that works fine for C++.

Java is not C++ so you should not expect that something that is valid in C++ is equally valid in Java.

If you want to update the value stored under a certain key in a map, you'll have to put it in the map again with the new value, which will replace the old value. For example:

Map<String, Integer> map = new HashMap<>();
map.put("a", 1);

// Get the value stored under "a", add one and store the new value
map.put("a", map.get("a") + 1);
Jesper
  • 202,709
  • 46
  • 318
  • 350
-3

What you get as a result from the map.get is an Integer object. And those Integer objects do not support the += operator (as you know them from C++) as you cannot overload operators in Java generally. Instead you have to put it into the map again using the map.put function. Notice that an Integer object is different from an built-in int type!

sleepy1771
  • 313
  • 1
  • 8
  • Actually, this does work and does what you would expect it to. – Tunaki Sep 05 '15 at 19:45
  • 1
    Your code that "does not work" does actually work, due to autounboxing and autoboxing. The problem is nothing to do with integer operations here - it's that you can't assign to the return value of a method. – Jon Skeet Sep 05 '15 at 19:46
  • Yeah you two are right. I still had some other compiler flags turned on that prevented Autoboxing. I'm sorry for that... – sleepy1771 Sep 05 '15 at 19:52