Skip to content

Instantly share code, notes, and snippets.

@Keruspe
Created June 27, 2011 12:48
Show Gist options
  • Save Keruspe/1048790 to your computer and use it in GitHub Desktop.
Save Keruspe/1048790 to your computer and use it in GitHub Desktop.
Why I do hate java
public class Ref {
public void inc1(int i) {
++i;
}
public void inc2(Integer i) {
++i;
}
public static void main(String[] args) {
int foo = 32;
Integer bar = 41;
Ref r = new Ref();
System.out.println("Expected: 32x41\nGot: " + foo + "x" + bar);
r.inc1(foo);
System.out.println("Expected: 32x41\nGot: " + foo + "x" + bar);
r.inc2(bar);
System.out.println("Expected: 32x42\nGot: " + foo + "x" + bar);
r.inc1(bar);
System.out.println("Expected: 32x42\nGot: " + foo + "x" + bar);
r.inc2(foo);
System.out.println("Expected: 32x42\nGot: " + foo + "x" + bar);
/* Output:
Expected: 32x41
Got: 32x41
Expected: 32x41
Got: 32x41
Expected: 32x42
Got: 32x41
Expected: 32x42
Got: 32x41
Expected: 32x42
Got: 32x41
*/
}
}
@kepstin
Copy link

kepstin commented Jun 27, 2011

Oh, I see… I wasn’t quite paying enough attention. Yes, it is odd how Integer is an effectively immutable object like that, which is different from most other Java objects.
I’m not a particularly big fan of Java myself, or I might have noticed that sooner ☺

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment