why the result is false? could someone please explain?
public class StringTest1 {
public static void main(String[] args) {
String a="a";
String b=a+"b";
String c="ab";
System.out.println(b==c);
}
}
why the result is false? could someone please explain?
public class StringTest1 {
public static void main(String[] args) {
String a="a";
String b=a+"b";
String c="ab";
System.out.println(b==c);
}
}
Because they don't point to the same object in the memory.
==
is used for comparison of either primitive types, or object references.
What you want to do, is to compare their values, for which you 'll need to use the equals(Object o)
or equalsIgnoreCase(Object o)
method(s)
Output of this comparison is
FALSE
because you have created two objects which have different location in heap so==
compare their reference or address location and return false.