Why are these two not equal? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why are these two not equal?

String str1 = new String("solo"); String str2 = new String ("solo"); if(str1==str2) System.out.println("equal"); It has no output. But if we run this, it will print equal. String str1 = "solo"; String str2 = "solo"; if(str1==str2) System.out.println("equal"); Explain the logic behind this.

24th Jan 2020, 6:45 AM
Abhinav Shukla
Abhinav Shukla - avatar
1 Answer
+ 5
https://www.geeksforgeeks.org/difference-equals-method-java/ Read that link. " == " checks if the string is the same, meaning that both sides point to the same object. The method .equals() checks if the value is the same.
24th Jan 2020, 6:51 AM
Fernando Pozzetti
Fernando Pozzetti - avatar