This week's giveaway is in the EJB and other Java EE Technologies forum. We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line! See this thread for details.
When you use == between two object references, it test whether or not the references point to the same object in memory.
When you use .equals between two object references, then either an overridden equals method is called to determine what the programmer means by the two objects being equal or the equals inherited from Object is called, in which case it is the same as ==.
In the above code when you use == to compare 'a' and 'b' it will return false since these two are different objects but a.equals(b) will return true since they both have same contents [ May 05, 2006: Message edited by: Srinivasa Raghavan ]
the 'foo' is not an object, but a reference to an object. foo basically says "look over here in THIS spot for the real thing".
so when you have two references, foo and bar, the == says "do they both refer to the same spot?"
the equals() method says "use the method defined to figure out if what each one refers to are equilvelant." they may both point to the same bin, in which case the answer is yes.
but if one points to spot A, and the other points to spot B, it's possible that the two different things in those spots are equivilant. they are the same type of object, with the same values in the member variables, etc.
the thing about equals() is that while every class has a version by default (possibly inherited from the Object class, possibly defined somewhere else), you can define your own for classes you create. YOU can decide what makes two instances of your class "equal".
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Joined: Apr 27, 2006
Hi all, Thank you very much for clarification.I got it! Once again Thanks a lot!
Hi Guys, I understand that equals() compares values of two string and == compares references. From this ___________________________________________________________________________ String str1 = "A"; String str2 = "A"; System.out.println(str1+" equals() "+str2+" "+(str1.equals(str2))+" == "+(str1 == str2)); ___________________________________________________________________________
The output should be A equals A true == false
But the eclipse and netbeans give me output as: A equals() A true == true.
But if I change this to: _______________________________________________________________________ String str1 = new String("A"); String str2 = new String("A"); System.out.println(str1+" equals() "+str2+" "+(str1.equals(str2))+" == "+(str1 == str2)); _______________________________________________________________________
The output is A equals() A true == false
I will like to ask that what is difference here between String str1 = "A" and String str1 = new String("A"). Is a new reference not created when we do str1 = "A"??
A string is often created from a string literal � a series of characters enclosed in double quotes. The shortcut syntax for instantiating new String objects from string literals:
String s = "A";
This special shortcut syntax was designed to improve String performance: The JVM sets aside a special area of memory called the "String constant pool". Each JVM only keeps one copy of each string literal. If the compiler encounters a String literal, and the String literal has been created in String constant pool, the reference to the new String literal is directed to the existing String.
So String s = "A"; String s1 = "A"; "A" is in the String pool, the compiler will assign s and s1 to the same String object in String pool.
Please revise your display names to meet the JavaRanch Naming Policy. To maintain the friendly atmosphere here at the ranch, we like folks to use real (or at least real-looking) names, with a first and a last name.