0
public class practice {
public static void main(String[] args) {
    String str1 = new String("abcdef");
    String str2 = new String("abcdef");
    System.out.println(str1==str2);
        }
}

can somebody explain why is it returning false even though they have the same string content .

  • 2
    You're attempting to compare the memory address of the two objects, not there contents. You need to use `str1.equals(str2)` instead. Have a look at [Comparing Strings and Portions of Strings](http://docs.oracle.com/javase/tutorial/java/data/comparestrings.html) – MadProgrammer Apr 02 '13 at 04:32
  • Duplicate of http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – default locale Apr 02 '13 at 04:32
  • You tagged this with "java string comparison." Did you try searching the site for those terms? – yshavit Apr 02 '13 at 04:33

0 Answers0