Question on Strings
In the below code, why is it that in the first test the comparison results in false and in the second one it returns true...Can someone please explain what happns behind the scenes..
publicclass StringTest{
String st =new String("123");
String st1 =new String("123");
publicstaticvoid main(String[] args){
StringTest obj =new StringTest();
/*obj.st="123";
obj.st1="123";
System.out.println(obj.st == obj.st1);*/
obj.anotherMethod();
}
publicvoid anotherMethod(){
System.out.println(st==st1);
st ="123";
st1 ="123";
System.out.println(st==st1);
}
}
http://www.ibm.com/developerworks/java/library/j-ebb0917a.htmlRead the above which should answer your questions, if you still have doubts then post back.
if you want to compare the content of the Strings use equals() methodIf you want to check if the reference 1 is the same object of reference 2, use == operator
Hi to all,
It is very clear before this i want to explain you the diff. between
String s=new String("1234"); and String s1="1234";
In the first case s is created as a object
and in the second case s1 is created in string pool which is maintend by JVM.
String s1="1234";
String s2="1234";
if we create same string twice in stringpool it can identified it as same So it does not create another object for second one, So these two will be point to same location in memory.
whenevr you are comparing s1==s2 it will search the address in pool so it will be same in this case.
Corlettk,I am relatively new to Java..what is the difference between == and .equals, because in all my past programming, i have used ==, and to be honest I am still using that in Java (most of the time)Thanks
> http://www.ibm.com/developerworks/java/library/j-ebb09> 17a.html> > Read the above which should answer your questions, if> you still have doubts then post back.Thanks so much.
> Hi to all,
> It is very clear before this i want to explain you
> the diff. between
>
> String s=new String("1234"); and String s1="1234";
>
> In the first case s is created as a object
> and in the second case s1 is created in string
> pool which is maintend by JVM.
> String s1="1234";
> String s2="1234";
>
> if we create same string twice in stringpool it can
> identified it as same So it does not create another
> object for second one, So these two will be point to
> same location in memory.
>
> whenevr you are comparing s1==s2 it will search the
> address in pool so it will be same in this case.
Thank you.
You should also try making your variables final and comparing the results with non final variables in comparison.
Or in short...
string1 == string2 is comparing the two objects... as in, is this object the same object as that object... probably not what you expected or intended.
string1.equals(string2) compares the contents of the two strings... which is probably what you intended, just expressed differently than you expected.
a.equals(b) seems weird at first, but you'll get used to it quickly, and (trust me) it's just SOOOOOO much cleaner than overloading operators.
Cheers, Keith.
Message was edited by: corlettk
short and sweetthanks!And Siva, sounds like you got the help you needed, make sure you hand out the reward promised..Message was edited by: Masterkeedu
> You should also try making your variables final and> comparing the results with non final variables in> comparison.I don't think it would change anything here.The thing happening here is about what krosuru wrotes.
ibanna at 2007-7-12 22:45:45 >

> == != equalsThat is true, and even this holds true: ! == .equals (equals )
> Or in short... > > string1 == string2 is comparing the two objectsIt compares reference values, not objects.
jverda at 2007-7-12 22:45:45 >

> Hi to all,
> It is very clear before this i want to explain you
> the diff. between
>
> String s=new String("1234"); and String s1="1234";
>
> In the first case s is created as a object
> and in the second case s1 is created in string
> pool which is maintend by JVM.
Just to clarify: Executing s1 = "1234" does NOT create the string in the pool. That is done when the class is initialized. Executing that line simply assigns a reference to that existing String object to the variable s1.
jverda at 2007-7-12 22:45:45 >
