+ 2

Java

What am I doing wrong here? String title1 = new String(“cse220”); String title2 = “cse220”; if (title1 == title2) { System.out.print(“String using comparison “==“ sign works”); } else { System.out.print(“String comparison using “==“ does NOT work”); } if (title1.equals(title2)) { System.out.print(“String comparison using “equals” method works”); } else { System.out.print(“String comparison using “equals” method does NOT work”); }

31st Aug 2021, 4:25 AM
Robert Hinojosa
Robert Hinojosa - avatar
3 odpowiedzi
+ 4
Yoseph To add double quotes to your string, you can use escape sequence (\"). Otherwise it will cause error.Like you have given ("String comparison using "==" does NOT work"); Here the text inside double quotes are treated as string.The two strings will be, 1."String comparison using" 2."does NOT work" But == is not in any string it will cause error. Here is the modified code String title1 = new String("cse220"); String title2 = "cse220"; if (title1 == title2) { System.out.print("String using comparison \"==\" sign works"); } else { System.out.print("String comparison using \"==\" does NOT work"); } if (title1.equals(title2)) { System.out.print("String comparison using equals method works"); } else { System.out.print("String comparison using equals method does NOT work"); } Thnx
31st Aug 2021, 6:08 AM
Jewel
Jewel - avatar
+ 1
Awesome thank you so much !! Jewel
31st Aug 2021, 6:35 AM
Robert Hinojosa
Robert Hinojosa - avatar
0
You're welcome Yoseph 👍
31st Aug 2021, 6:58 AM
Jewel
Jewel - avatar