1

When I compare string s1 and s2 it give equal result but with s3 it gives a different result

String s1="Hello"
String s2="Hello"
String s3=new String("Hello");

Output

s1==s2 //true
s1.equals(s2) //false
s1==s3 //false
s1.equals(s3) //true

why does it produce different output even if the string is the same?

disamaj964
  • 23
  • 4
  • 2
    `s1.equals(s2)` is `true`. How did you test when it produced `false`? – ernest_k Nov 01 '21 at 06:39
  • You essentially never need to use `new String(String)`. Strings are immutable, so there is no need to have distinct instances with the same value. – Andy Turner Nov 01 '21 at 07:28

0 Answers0