1

I was going through 'equal' method concept in effective java, and there I found below code:

@Override
public boolean equals(Object o) {
    if (o instanceof CaseInsensitiveString)
        return s.equalsIgnoreCase(((CaseInsensitiveString) o).s);
    if (o instanceof String)  // One-way interoperability!            
        return s.equalsIgnoreCase((String) o);
    return false;
}

Here I am not able to get particular line that is ((CaseInsensitiveString) o).s . Now what I understand from this piece of code is object 'o' is been typecast to CaseInsensitiveString Class. Now what does ).s mean.

Juan Carlos Mendoza
  • 5,736
  • 7
  • 25
  • 50
Naved Ali
  • 616
  • 1
  • 14
  • 31

2 Answers2

5

You can't read the ).s in isolation:

return s.equalsIgnoreCase(((CaseInsensitiveString) o).s); 

Is like:

CaseInsensitiveString c = (CaseInsensitiveString) o;
return s.equalsIgnoreCase(c.s);

It's just accessing a field from the instance of CastInsensitiveString.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
  • 2
    And OP's confusion about what `.s` means is a great example of why you never have public members with names like `s`. If it were a more meaningful name, it might have been more obvious. – Mike Harris Oct 26 '17 at 13:36
2

The class CaseInsensitiveString has a member s of type String.

To access that member, you need to

  • cast the Object o to CaseInsensitiveString using ((CaseInsensitiveString) o)
  • then access the field s using ((CaseInsensitiveString) o).s.
f1sh
  • 11,489
  • 3
  • 25
  • 51