Petit rappel de prog' Java :
« For reference types, == checks whether the references are equal, i.e. whether they point to the same object.
For primitive types, == checks whether the values are equal.
java.lang.Integer is a reference type. int is a primitive type.
Edit: If one operand is of primitive type, and the other of a reference type that unboxes to a suitable primitive type, == will compare values, not references. »
Comparer deux String : .equals()
Comparer deux Integer ou deux Long : .equals()
Comparer deux int ou deux long : ==
Comparer un Integer ou Long avec un int ou un long : ==
C'est parfois beaucoup plus fourbe que ça :
Map<Long, String> test = new HashMap<Long,String>();
test.put((long) 666, new String("lala"));
System.out.println(test.get(666));
System.out.println(test.get((long) 666));
=> la méthode put() nous impose de caster, mais pas la méthode get() donc le premier sysout affichera null alors que le deuxième retournera le résultat attendu. :P
Thu May 7 12:37:56 2015 - permalink -
-
http://stackoverflow.com/questions/12547201/comparing-integer-objects