6080 links
  • GuiGui's Show
  • Home
  • Login
  • RSS Feed
  • ATOM Feed
  • Tag cloud
  • Picture wall
  • Daily
Links per page: 20 50 100
 
  • java - Comparing Integer objects - Stack Overflow
    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
    May 7, 2015 at 12:37:56 PM UTC - permalink - http://stackoverflow.com/questions/12547201/comparing-integer-objects
Links per page: 20 50 100
 
Mentions légales identiques à celles de mon blog | CC BY-SA 3.0

Shaarli - The personal, minimalist, super fast, database-free, bookmarking service by the Shaarli community