+ 1

What did I miss?

public class StringCompare{ public static void main(String []args){ String s1 = "hey!"; String s2 = "hello"; if (s1 == s2){ System.out.println("The two texts are same"); } else { System.out.println("Nope! They are different"); } } }

28th Nov 2017, 11:37 AM
Rizan
Rizan - avatar
3 Answers
+ 10
Don't use == to compare strings, use equals() method instead. == returns true only if the two strings are same object (must occupy same memory address). Code: https://code.sololearn.com/cA7blCcjcnE3/#java
28th Nov 2017, 12:39 PM
Shamima Yasmin
Shamima Yasmin - avatar
+ 1
Turns out I missed a curly brace at the end(in the compiler- corrected here). Are there any modifications I can make to this simple code? Can somebody show me how I can take user-provided values for s1 and s2? Thanks!
28th Nov 2017, 11:39 AM
Rizan
Rizan - avatar
+ 1
Thanks!!
28th Nov 2017, 3:03 PM
Rizan
Rizan - avatar