i++ fails ( ... java bashing )

johanvdb on 2003-08-08T00:22:28

I did not make it to YAPC::Europe::2003 ... that is a bit of a sad thing, but hey, my daugther got sick on thursday evening - nothing serious , though - so my wife was very happy having me around. So I guess that not going to YAPC was rather a good thing!

Anyhow ... I'm a bit annoyed with java, although i need to do *stuff* with it for my day job. The last week I've been busy using aspects (AspectJ, http://www.eclipse.org/aspectj) to do some code coverage profiling on file system access. Anyhow, I was a bit thorn away by the fact that the next piece of code does not work:

Integer i = new Integer(5); i++;

This does not work, neither has the Integer object an add, substract or Integer.doSomethingUsefull() method. Curious not, that a language which was originally targetted to embedded systems has so much trouble getting its primitive types in order. I need to have an integer object, because I needed top keep a hash (HashMap), linking a name to a counter (${$key}++) .... it expects an Object as its key and value paremeter in the put method (HashMap.put(key, value)). Anyhow, a colleague told me he uses an array with one element for this. A straight int type will not work, as it is a primitive integer type and no Integer object.

Johan


one trick...

educated_foo on 2003-08-08T15:14:16

...is to use a one-element array. Then with the minor pain of an extra "[0]", you can use normal arithmetic ops. Of course, you still have to either cast the result of get() or create a new subclass of HashMap that does the casting for you. There's no silver bullet for Java's loathesomeness. /s

BTW, the one-element array trick can also be used to get around the stupid restriction about not allowing access to non-final variables in anonymous classes.