Why String s1==s2 return false if I add +"" and return True If I used s1==s2 -
public class maindemo { public void comp() { string s1 = "abc"; string s2 = "abc"; system.out.print(""+s1==s2); // why return false??? plz clear doubt? system.out.println(s1==s2);//and why true } /** * @param args */ public static void main(string[] args) { // todo auto-generated method stub maindemo obj=new maindemo(); obj.comp(); } } ##################################################### why return false ??
system.out.print(""+s1==s2); // why return false??? please clear doubt?
edited
can tell me how check instance value
edited 2
system.out.println(s1.hashcode()); // both same
system.out.println(s2.hashcode());// both same
then happened this?????
comparing string not idea, use a1.equals(a2); answer of question.
string a1="abc"; string a2="abc"; system.out.println(a1==a2); // true system.out.println(""+a1==a2); // false system.out.println(""+(a1==a2)); // true look @ ""+a1. if try ""+a1==a1 return false, confused? don't because ""+a1 new string. while ""+(a1==a2) compares first , append :""+(true)
as suggested use a1.equals(a2); instead of == strings
official: here
equals(object anobject) compares string specified object.
Comments
Post a Comment