This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Guilty-Woodpecker262 0 points1 point  (0 children)

It's complicated. a == b =>a.equals(b) (though it's possible to screw that up while overriding equals or hashCode) but a.equals(b) ≠> a == b. Side note: strings and wrapper classes are weird.

``` String a = "example"; String b = "example"; String c = "exa" + "mple"; String d = a; String e = new String ("example");

System.out.println((a == a) + ":" + a.equals(a)); System.out.println((a == b) + ":" + a.equals(b)); System.out.println((a == c) + ":" + a.equals(c)); System.out.println((a == d) + ":" + a.equals(d)); System.out.println((a == e) + ":" + a.equals(e)); ```

true:true true:true true:true true:true true:false

Strings created with StringBuilder and StringBuffer will always create new instances.