1
public class a {
public static void main(String args[]){
final String s1="job";
final String s2="seeker";
String s3=s1.concat(s2);
String s4="jobseeker";
System.out.println(s3==s4);=> false
System.out.println(s3.hashCode()==s4.hashCode());=>true
System.out.println(s3.hashCode());=>2201324
System.out.println(s4.hashCode());=>2201324

}
}

"==" compare hashCode of the Object... .hashCode of the s3 and s4 are the same but s3==s4 gives false . Anyone explain. I need an answer not useless question not in correct format comments. people who do not know answer keep away from this question.

manlio
  • 18,345
  • 14
  • 76
  • 126

1 Answers1

0
  1. Java Strings can't be compared using == operator, because it only compares references. Use s3.equals(s4) for string comparison overload.
  2. I guess that s3.hashCode() == s4.hashCode() evaluates to true because it compares integers, which are compared by value in Java.
Mariusz
  • 481
  • 2
  • 4
  • 11
  • s3.hashCode() == s4.hashCode() evalutase to true, because hashCode() returns an int, which can be compared with == – MTilsted May 03 '14 at 08:58
  • Right, obviously :) Anyway, ints can be compared with ==, strings not (as long as you're interested in contents, not reference). – Mariusz May 03 '14 at 09:03