Javaequals, hashCode, toString                        2011 4    2                      twitter: @zaki50
• Effective Java       2  • ISBN:978-4-89471-499-1  • 3,780     (    )  •      8,       9,       10
•                  equals(Object)• equals(Object)     hashCode()• toString()
Equals
Equals   ==• ==                (  null)• equals
Map   put            get
override•                                       (   : Thread)•    (   :enum)•                                             ...
override    equals
Object                          equals•• public boolean equals(Object obj) {        return (this == obj);    }
equals• x.equals(x) == true          x != null && y != null                                    && z != null• x.equals(null...
private final int id;private final String name;@Overridepublic boolean equals(Object obj) {	 if (this == obj)	 	 return tr...
•                             equals                           equals•    (java.net.URL   ...)
hashCode
override • equals(Object)   hashCode()
hashCode• equals• x.equals(y)          x.hashCode() ==  y.hashCode()• !x.equals(y)  x.hashCode() != y.hashCode()          ...
private final int userId;private final String name;@Overridepublic int hashCode() {	 final int prime = 31;	 int result = 1...
ToString
ToString•••
private final int id;private final String name;@Overridepublic String toString() {	 return "GoodClass [userId=" + userId +...
• IDE• commons-lang   EqualsBuilder/ HashCodeBuilder/ToStringBuilder
IDE
@Override	   public int hashCode() {	   	   final int prime = 31;	   	   int result = 1;	   	   result = prime * result + ...
commons-lang• EqualsBuilder/HashCodeBuilder/  ToStringBuilder •                     reflection* •
•                  equals(Object)• equals(Object)     hashCode()• toString()
ぐだ生 Java入門第一回(equals hash code_tostring)
ぐだ生 Java入門第一回(equals hash code_tostring)
Upcoming SlideShare
Loading in …5
×

ぐだ生 Java入門第一回(equals hash code_tostring)

2,928 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,928
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
11
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • ぐだ生 Java入門第一回(equals hash code_tostring)

    1. 1. Javaequals, hashCode, toString 2011 4 2 twitter: @zaki50
    2. 2. • Effective Java 2 • ISBN:978-4-89471-499-1 • 3,780 ( ) • 8, 9, 10
    3. 3. • equals(Object)• equals(Object) hashCode()• toString()
    4. 4. Equals
    5. 5. Equals ==• == ( null)• equals
    6. 6. Map put get
    7. 7. override• ( : Thread)• ( :enum)• ( : Random)• ( :HashSet(AbstractSet ))• equals • AssertionError
    8. 8. override equals
    9. 9. Object equals•• public boolean equals(Object obj) { return (this == obj); }
    10. 10. equals• x.equals(x) == true x != null && y != null && z != null• x.equals(null) == false• x.equals(y) == y.equals(x)• x.equals(y) && y.equals(z) => x.equals(z)• setter
    11. 11. private final int id;private final String name;@Overridepublic boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Sample other = (Sample) obj; if (id != other.id) return false; if (name == null) { if (other.name != null) equals(Object) return false; } else if (!name.equals(other.name)) return false; return true;}
    12. 12. • equals equals• (java.net.URL ...)
    13. 13. hashCode
    14. 14. override • equals(Object) hashCode()
    15. 15. hashCode• equals• x.equals(y) x.hashCode() == y.hashCode()• !x.equals(y) x.hashCode() != y.hashCode() x != null && y != null
    16. 16. private final int userId;private final String name;@Overridepublic int hashCode() { final int prime = 31; int result = 1; result = prime * result + userId; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result;} hashCode()
    17. 17. ToString
    18. 18. ToString•••
    19. 19. private final int id;private final String name;@Overridepublic String toString() { return "GoodClass [userId=" + userId + ", name=" + name + "]";}
    20. 20. • IDE• commons-lang EqualsBuilder/ HashCodeBuilder/ToStringBuilder
    21. 21. IDE
    22. 22. @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + id; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Sample other = (Sample) obj; if (id != other.id) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; }
    23. 23. commons-lang• EqualsBuilder/HashCodeBuilder/ ToStringBuilder • reflection* •
    24. 24. • equals(Object)• equals(Object) hashCode()• toString()

    ×