Recently I stumbled across a small but annoying difference in border cases of hex literals between Groovy and Java
In Groovy, this
println(0xff000000.class)
will yield class java.lang.Long
In Java, this
Object x = 0xff000000; System.out.println(x.getClass());
will yield java.lang.Integer
In Groovy, the literal is a long because it’s too big to fit into an Integer (that’s true, 4278190080 is well above 2^31 and thus a Long), but in Java it’s -16777216, which is an Integer.
The solution is to suffix the constant with an I, e.g. 0xff000000I, then Groovy leaves it as an Integer (Java doesn’t support that notation, though).
This is relevant when doing bit operations, e.g. the expression
(-1 & 0xff000000) >> 24
is 255 in Groovy, and -1 in Java
Thank goodness you don’t need bit twiddling too much this days …