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 |
|
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);
}Math
| Code | Result | Math notation |
|---|---|---|
|
0.8939966636005579 == sin(90.0) |
|