Can any body help me with this problem? It's on Oca book... the answer is (equal) but I don't know why...? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can any body help me with this problem? It's on Oca book... the answer is (equal) but I don't know why...?

String a = ""; a += 2; a += 'c'; a += false; if ( a == "2cfalse") System.out.println("=="); if ( a.equals("2cfalse")) System.out.println("equals");

20th Mar 2020, 12:28 PM
zahra.j
zahra.j - avatar
2 Answers
+ 1
In java, '==' compares refferences not content. equals() method compares content(values)...
20th Mar 2020, 1:45 PM
Jayakrishna 🇮🇳
0
Because == is for comparing numbers. For comparing String, we need to use the equals() method. https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
20th Mar 2020, 12:34 PM
Gordon
Gordon - avatar