Hi Rohit, s1 is indeed EXACLY the same as s2, because the instruction "s2 = s1" means "s2 now points to the same place as s1". I think that's just a problem of operator precedence. Well, let's see:
First (it's between brackets): s1==s2 --> "Hello" == "Hello" --> true Second (aditive operators like "+" come before equality operators like "=="): true + " " --> "true " Third (next "+" operator): "true " + s1 --> "true Hello" Last (the "==" operator comes after, as i said): "true Hello" == s2 --> "true Hello" == "Hello" --> false Checkout this out: http://www.javapassion.com/javaintro/#Programming_fundamentals (operator precedence at the last slides) http://java.sun.com/docs/books/tutorial/java/nutsandbolts/operators.html Hope I'm not wrong with this (I couldn't try this here at work) Hugs On Mon, Jul 20, 2009 at 4:42 PM, Nimisha Sinha<[email protected]> wrote: > In Java,String literals are stored in a literal pool.If two string literals > are having same content,their references will be same too. > > On Mon, Jul 20, 2009 at 8:39 AM, miga <[email protected]> wrote: >> >> >> >> On Jul 20, 5:32 pm, Rohit Bansal <[email protected]> wrote: >> > Let I had following code snippet, >> > String s1="Hello"; >> > String s2=s1; >> > System.out.println((s1==s2)+" "+ s1==s2); >> > >> > OUTPUT: false >> > >> > Query: I need understanding of output...its strange for sure but true; >> When you use == for String, you state that the Strings have the same >> contents AND the same reference. >> Here contents are the same, but references are not. >> If you want to compare contents, use equals method. >> >> > -- Diogo Sales Oliveira --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/javaprogrammingwithpassion?hl=en -~----------~----~----~----~------~----~------~--~---
