I am trying to solve this but it returns false. HashCode and data in both literal is the same if
String s1="java";
String s2="JAVA".toLowerCase();
System.out.println(s1==s1);
I am trying to solve this but it returns false. HashCode and data in both literal is the same if
String s1="java";
String s2="JAVA".toLowerCase();
System.out.println(s1==s1);
public class HelloWorld{
public static void main(String []args){
System.out.println("Hello World");
String s1="java";
String s2="JAVA".toLowerCase();
System.out.println(s1.hashCode());
System.out.println(s1.hashCode());
System.out.println(s1==s2);
System.out.println(s1.equals(s2));
}
}
.equals() will compare the value of variables.
== will compare the value of variable memory locations.
Hashcode is calculated based on the content of the string and not the location of string.
As you are using String class, it is of reference type
find documentation here