Hi, Try this - str1="Hello".intern(); str2="Hello".intern(); Now according to docs, str1==str2 should return true. - Rajesh -----Original Message----- From: fgs [mailto:[EMAIL PROTECTED]] Sent: Tuesday, November 30, 1999 4:31 AM To: [EMAIL PROTECTED] Subject: Comparing Strings Yes, Mr Craig, What we want is when str1="Hello" str2="Hello" points to the same object reference given by the documentation then str1==str2 should return true but it does not Why? This is our question............. FGS Infotech Private Limited ----- Original Message ----- From: Craig R. McClanahan <[EMAIL PROTECTED]> To: fgs <[EMAIL PROTECTED]> Cc: <[EMAIL PROTECTED]> Sent: Tuesday, November 30, 1999 2:15 AM Subject: Re: request.getParameter() in an if statement > fgs wrote: > > > nOT ONLY THAT WE ALSO have the same problem with > > > > Case 1: > > String str="Hello"; > > String str2="Hello"; > > > > Case 2: > > String str=new String("Hello"); > > String str2=new String("Hello"); > > > > Java Documentation says that in case 1, The JVM does not create an object > > for str2 but passes the same memory reference of str to str2. Only in the > > second case it creates differednt objects for two variables. > > If this is so , then for the first case str == str2 must return true. But > > this does not happen? We get confused with this. > > > > But the statement str2.equals(str) will return true in both the cases? > > > > Any Java Team member can answer this question? > > > > Please see the API documentation for the equals() method, first for > java.lang.Object and then for java.lang.String. It is up to a class to define for > itself what one object being "equal to" another means. In the case of strings, it > means that the two strings represent the same sequence of characters. In both of > the above cases, this test passes so they both return "true". > > For your own classes, you have the choice of overriding equals() to test what you > want. If you do not override it, equals() defaults to the test included in > java.lang.Object, which says two references are equal if they refer to the same > exact object instance. For example: > > Given: > > MyClass obj1 = new MyClass(...); > MyClass obj2 = new MyClass(...); > MyClass obj3 = obj1; > > Then: > > (obj1 == obj2) returns false -- they are different instances. > (obj1 == obj3) returns true -- they are the same instance. > > Using an equals() test would return the same results if you do not override it. > > > > > -FGS Infotech Private Limited...... > > > > Craig McClanahan > > > =========================================================================== To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST". FAQs on JSP can be found at: http://java.sun.com/products/jsp/faq.html http://www.esperanto.org.nz/jsp/jspfaq.html =========================================================================== To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST". FAQs on JSP can be found at: http://java.sun.com/products/jsp/faq.html http://www.esperanto.org.nz/jsp/jspfaq.html