Skip to content

Instantly share code, notes, and snippets.

@TGITS
Last active January 22, 2022 15:51
Show Gist options
  • Save TGITS/78f4b9e3508aea23bd2805e450ec7fa1 to your computer and use it in GitHub Desktop.
Save TGITS/78f4b9e3508aea23bd2805e450ec7fa1 to your computer and use it in GitHub Desktop.
Exemple de fonctionnement des méthodes equals et hashCode des records
//A exécuter sous JShell : /open RecordExampleScript_2.java
public record House(String name, String seat, String sigil, String familyWords) {
}
House house1 = new House("Stark", "Winterfell", "A Gray Direwolf", "Winter is coming");
House house2 = new House("Stark", "Winterfell", "A Gray Direwolf", "Winter is coming");
house1 == house2; // false - 2 références distinctes
house1.equals(house2); // true - comparaison basée sur le contenu
house1.hashCode() == house2.hashCode(); // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment