Re: switch using strings
On 3/2/2011 10:23 PM, javax.swing.JSnarker wrote:
On 02/03/2011 9:18 PM, Eric Sosman wrote:
Not so long ago, somebody asked for help here with an "obviously
correct" construct that wasn't working right. Paraphrasing:
public class Paraphrase {
private Integer counter = 0;
public void increment() {
synchronized(counter) {
++counter;
}
}
}
IMO, if this does *not*:
* start counter as an Integer whose value is 0
* lock it before each increment
* unbox it, add one, box it again, and reassign
* and then release the monitor
That is precisely what it does. See the bug yet?
then that's actually a language bug, or at least a wart.
Yes, IMHO, even though it behaves exactly as you describe.
"++x" is
supposed to be equivalent to "x = x + 1";
It is.
here, "x + 1" would require
unboxing and "x = ..." would then require boxing again.
Yes.
What does it do
instead, increment the temporary but fail to reassign?
No.
Refuse to compile
++<reference> in general?
Still no.
Re-write the code without auto-boxing/unboxing -- pretend you're
using a pre-1.5 Java -- and I'm sure you'll see the problem. (And
since you didn't see it right away, I think that lends support to the
notion that autoboxing is, or at any rate can be, "dangerous.")
It seems whatever the exact problem was it could have been avoided by
making a different choice in the implementation of some aspect of the
language.
Agreed. For example, choosing not to have autoboxing.
--
Eric Sosman
esosman@ieee-dot-org.invalid