Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
ObjectLecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
(#1 of 4) |
Lecture notes |
Pdf slides |
|
(#2 of 4) |
Lecture notes |
Pdf slides |
|
(#3 of 4) |
Lecture notes |
Pdf slides |
|
(#4 of 4) |
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
(#1 of 6) |
Lecture notes |
Pdf slides |
|
(#2 of 6) |
Lecture notes |
Pdf slides |
|
(#3 of 6) |
Lecture notes |
Pdf slides |
|
(#4 of 6) |
Lecture notes |
Pdf slides |
|
(#5 of 6) |
Lecture notes |
Pdf slides |
|
(#6 of 6) |
equals()Lecture notes |
Pdf slides |
|
== vs. equals()Lecture notes |
Pdf slides |
|
equals()
implications
Lecture notes |
Pdf slides |
|
equals() is being defined within
respective class!
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
equals()Lecture notes |
Pdf slides |
|
Rectangle
equals(...) and
hashCode()Lecture notes |
Pdf slides |
|
Rectangle hash values
Lecture notes |
Pdf slides |
|
Improved
hashCode() method
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Math.sin(double
x)Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Working with class String.
Pitfalls when using operator ==
Using equals(...).
Superclass of all Java™ classes.
Common methods to be redefined by derived classes.
Implementation of java.lang.String:
public final class String ... {
private final char value[];
private int hash;
private static final long serialVersionUID = -6849794470754667710L;
...
}| Primitive type | Object |
|---|---|
|
|
==: true |
==: false equals: true |
The == operator acting on primitive
types compares expression values.
The == operator acting on objects
compares for equality of reference values and thus for object
identity.
The == operator acting on objects
does not check whether two objects
carry semantically equal values.
The equals()
method defines the equality two objects.
Each object is equal by value to itself:
object1 == object2 ⇒
object1.equals(object2)
The converse is not true. Two different objects may be of common value:
| Code | Result |
|---|---|
|
equals: true
==: false |
Implementation at https://github.com/openjdk/ .../String.java :
public final class String ... {
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
return (anObject instanceof String aString)
&& (!COMPACT_STRINGS || this.coder == aString.coder)
&& StringLatin1.equals(value, aString.value);
}Why using hash values?
hashCode() and
equals(...).
«Good» hashCode()
implementations.
![]() |
“I want the 12p one” |
Where is the blond haired guy?
I take the pink flower.
The 334.50$ cellular phone.
Method hashCode():
Instance 0 ⇨ o.hashCode(), of type int.
Same value on repeated invocation
Objects with identical value with respect to
equals() must have identical hash
values:
true == a.equals(b) ⟹ a.hashCode() == b.hashCode().
Conversely: Two instances differing with respect to
equals() may have identical hash
values.
Consequence: equals() and
hashCode() must be redefined simultaneously!
public class Rectangle {
int width, height;
@Override public boolean equals(Object o) {
if (o instanceof Rectangle r) {
return width == r.width && height == r.height;
} else {
return false;
}
}
@Override public int hashCode() {
return width + height;
}
}public class Rectangle {
int width, height;
...
@Override public int hashCode() {
return width + height;
}
} |
width | height | hash value |
|---|---|---|---|
| 1 | 3 | 4 | |
| 2 | 2 | 4 | |
| 5 | 5 | 10 | |
| 2 | 7 | 9 | |
| 4 | 9 | 13 |
public class Rectangle {
int width, height;
...
@Override public int hashCode() {
return width + 13 * height;
}
} |
width | height | hash value |
|---|---|---|---|
| 1 | 3 | 40 | |
| 2 | 2 | 28 | |
| 5 | 5 | 70 | |
| 2 | 7 | 93 | |
| 4 | 9 | 121 |
Math
| Code | Result | Math notation |
|---|---|---|
|
0.8939966636005579 == sin(90.0) |
|