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

}

}

[1284 byte] By [siva_charana] at [2007-11-27 9:31:15]
# 1
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.
_helloWorld_a at 2007-7-12 22:45:45 > top of Java-index,Java Essentials,Java Programming...
# 2
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
pbulgarellia at 2007-7-12 22:45:45 > top of Java-index,Java Essentials,Java Programming...
# 3
== != equals
corlettka at 2007-7-12 22:45:45 > top of Java-index,Java Essentials,Java Programming...
# 4

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.

krosurua at 2007-7-12 22:45:45 > top of Java-index,Java Essentials,Java Programming...
# 5
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
Masterkeedua at 2007-7-12 22:45:45 > top of Java-index,Java Essentials,Java Programming...
# 6
> 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.
siva_charana at 2007-7-12 22:45:45 > top of Java-index,Java Essentials,Java Programming...
# 7

> 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.

siva_charana at 2007-7-12 22:45:45 > top of Java-index,Java Essentials,Java Programming...
# 8
You should also try making your variables final and comparing the results with non final variables in comparison.
_helloWorld_a at 2007-7-12 22:45:45 > top of Java-index,Java Essentials,Java Programming...
# 9

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

corlettka at 2007-7-12 22:45:45 > top of Java-index,Java Essentials,Java Programming...
# 10
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
Masterkeedua at 2007-7-12 22:45:45 > top of Java-index,Java Essentials,Java Programming...
# 11
> 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 > top of Java-index,Java Essentials,Java Programming...
# 12
> == != equalsThat is true, and even this holds true: ! == .equals (equals )
BIJ001a at 2007-7-12 22:45:45 > top of Java-index,Java Essentials,Java Programming...
# 13
> Or in short... > > string1 == string2 is comparing the two objectsIt compares reference values, not objects.
jverda at 2007-7-12 22:45:45 > top of Java-index,Java Essentials,Java Programming...
# 14

> 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 > top of Java-index,Java Essentials,Java Programming...