At least for me, the quoted statement was even more confusing. As David said initially == is object comparison. That operator tests if both rhs and lhs (right/left hand side) referes to the exact same object. If you have any experience from C/C++ or any of the like, you know that the objects themselves reside in one memory space and the variable/name/alias you use is just a "pointer" containing the memory address of the object. If two variables (pointers) contain the same memory address, they are equal using the == operator. It is actually the memory addresses (such as 0x123456) that are compared. (Even if the naming conventions and more technical details may not be exactly the same for Java, the idea is the same).
The .equals() method compares the "called" object (lhs) and the parameter (rhs). Since it is a method it can (and is supposed to be) overridden by subclasses. (In C++ you can override the == operator). For the Object class, equals test for object equality using ==. For other classes (which all are subclasses of Object) the equals may (or rather should) work in some other way, preferably comparing the DATA. For String, it works by comparing the strings themselves. Therefore if two different String objects, located on different places in memory, contain the same string represantation (the same data), they will be equal using .equals(). For some other class, you might want to do some other kind of comparison when calling equals(). For a list or a set, you might want to see if they contain the same objects, maybe even in the same order. At least for me with C++ experience it helps a lot to think that == compares memory addresses (pointers) and .equals() compares the data. Examples (assuming the compiler is not optimizing this, which is another issue): String s1 = "A String"; String s2 = "A String"; s1 == s2 FALSE S1.equals(s2) TRUE Mattias Jiderhamn Expert Systems [EMAIL PROTECTED] > -----Original Message----- > From: David Nguyen [mailto:[EMAIL PROTECTED]] > Sent: Wednesday, December 19, 2001 6:55 PM > Subject: Re: .equals v.s. == > > More precisely, ".equals" is the IDENTITY testing > operator/method defined on > "String" objects (assuming "colors" is a String), and == is > the default > IDENTITY testing defined on java.lang.Objects. =========================================================================== To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST". For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST". Some relevant FAQs on JSP/Servlets can be found at: http://archives.java.sun.com/jsp-interest.html http://java.sun.com/products/jsp/faq.html http://www.esperanto.org.nz/jsp/jspfaq.jsp http://www.jguru.com/faq/index.jsp http://www.jspinsider.com
