1

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);

Reference image

Arvind Maurya
  • 910
  • 12
  • 25

1 Answers1

1
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));
     }
}

Output

.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

Arvind Maurya
  • 910
  • 12
  • 25
  • @akshayraut if you find the above answer helpful kindly respond it as accepted. – Arvind Maurya Jan 24 '20 at 07:16
  • Hello String s1="java"; String s2="java" and use s1==s2 then it return true.but see in above example there s2="JAVA".toLowerCase(); here also s2 store "java"(I print s2 on console) so why it return false. – Akshay Raut Jan 24 '20 at 11:29