In Java (unlike c++) the "==" operator compares *references* and not the value.
Therefore since the two strings are different objects they are !=.
Java doesn't provide for operator overloading as c++ does so you're stuck with
using String.equals(String) call. Also how can you say that str1 and str2 point to
the same object reference? They are not the same object.
>From API:
String str = "abc";
is equivalent to:
char data[] = {'a', 'b', 'c'};
String str = new String(data);
dave.
fgs wrote:
> 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
--
David Mossakowski [EMAIL PROTECTED]
Programmer 212.310.7275
Instinet Corporation
"I don't sit idly by, I'm planning a big surprise"
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html