• Core Classes

    Working with class String.

    Pitfalls when using operator ==

    Using equals(...).

  • Superclass of all Java classes.

  • Common methods to be redefined by derived classes.

Image layer 1
Image layer 2
Image layer 3
Image layer 4

Implementation of java.lang.String:

public final class String ... {
  private final char value[];
  private int hash;
  private static final long serialVersionUID = -6849794470754667710L;
...
}
Code Output
final String s = "Eve"; 
final String sCopy = new String(s); 
System.out.println("sCopy == s: " + (sCopy == s)); 
System.out.println("sCopy.equals(s): " + sCopy.equals(s)); 
sCopy == s: false 
sCopy.equals(s): true 
Image layer 1
Image layer 2
Image layer 3
Image layer 4
Image layer 5
Image layer 6
Primitive type Object
// equal values
int a = 12, b = 12;

System.out.println(
    "==: " + (a == b));
// No equals(...) method
// for primitive types
String 
 s1 = new String("Kate"),
 s2 = new String("Kate");

System.out.println(
  "    ==: " + (s1 == s2));
System.out.println(
  "equals: " + s1.equals(s2));
==: 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 == object2object1.equals(object2)

  • The converse is not true. Two different objects may be of common value:

    Code Result
    String s = "Hello", copy = new String(s);
    
    System.out.println("equals: " + s.equals(copy));
    System.out.println("    ==: " + (s == copy));
    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);
}
  • Core Classes
    • ➟ Objects, equals() and hash-values

      Why using hash values?

      hashCode() and equals(...).

      «Good» hashCode() implementations.

Hashing principle
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
  1. Choosing a good hashCode() method
  2. String and good hashCode() implementations.
  • Core Classes
    • ➟ Using class Math
Code Result Math notation
final double x = 90;
final double y = Math.sin(x);
System.out.println(y + " == sin(" + x + ")");
0.8939966636005579 == sin(90.0) 
y = sin x
  1. Common pitfall using trigonometric functions
  2. Using constants from java.lang.Math.
  3. Strings on CodingBat
  4. Masking strings
  5. Analyzing strings
  6. Pitfalls using ==: Equality of String instances
  7. Weird, weirder, weirdest!
  8. Analyzing file pathnames