Java: Clean way of avoiding NullPointerException in equals checks
问题 I have an address object that I want to create an equals method for. I could have made this quite simple by doing something like the following (shortened a bit): public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Address other = (Address) obj; return this.getStreet().equals(other.getStreet()) && this.getStreetNumber().equals(other.getStreetNumber()) && this.getStreetLetter().equals(other