Re: Updating an object in a HashMap
Arne VajhQj wrote:
The left hand side is not a literal.
It is an expression and you can not assign to an expression.
Lew wrote:
For example, the statements
int i;
(i) = 5;
would also fail to compile.
Mark Space wrote:
So does that mean that (i) is an r-value in Java? And are only
primitives (and references, being one type of primitive) assignable in
Java? Are primitives the only l-value in Java?
I'm not sure Java talks about r-values and l-values. The bottom line is that
only variables can have values assigned to them.
As an old C programmer myself, I think of variables as l-values, but really
that's not the Java terminology AIUI.
And autoboxing with the Object forms of the primitives looks like it's
been crowbarred to update the reference, not the Object....
I'm not sure exactly how you mean that. What do you mean by "crowbarred"?
There's no way to "update" a number wrapper class object like an Integer once
instantiated. So only the reference, i.e., the variable even can be updated.
Autoboxing causes a sequence like
Integer foo = new Integer( 17 );
foo += 23;
to assign a new Integer instance to foo. No Integer object is changed; the
Integer representing 17 is dropped and a new one referenced. Because values
from -128 to 127 are sort-of interned, and a larger range could be, there is
pretty likely no GC involved.
<http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.1.7>
--
Lew