Actually am testing the functionality of Scanner.
I have a very simple program which has a String variable which contains "abc".Then am reading other String(value "abc") from Scanner using next() method(even i tried with nextLine()).
Then am comparing using the ==, to check whether they are equal according to ==(i know i can compare with equals method which is working as excepted), the strange thing is that it is returning false when i compare using == , even though their hashcode() s are equal and equals() method returning true..
import java.util.Scanner;
public class Tester1234 {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
String str1="abc";
System.out.println("Eneter abc");
String str2=scanner.next();
System.out.println("str1.hascode()"+str1.hashCode()+"\tstr2.hascode()"+str2.hashCode()+"\tstr2.equals(str1)"+str1.equals(str2));
if(str1==str2)
{
System.out.println("equal");
}
else
{
System.out.println("not equal");
}
}
}
I want to know why it is behaving like this ??
Thanks...!