"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Originally posted by Kaydell Leavitt:
marc's comments made me wonder whether my examples were correct or not so I verified that they are correct:
Joanne
Srinvasa Reddy Thamma,<br />Chennai,India.
I am expecting "Both objects are equal" as the response but I am getting "unequal objects ". Can someone explain why ?
SCJP 5.0, SCJD in progress
SCJP 5.0, SCJD in progress
Srinvasa Reddy Thamma,<br />Chennai,India.
Srinivas Redd wrote:Thank you all for quick response. I thought equals() and == are different by default.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Srinivas Redd wrote:I have the below code.
I am expecting "Both objects are equal" as the response but I am getting "unequal objects ". Can someone explain why ?
[edit]Add code tags. CR[/edit]
[ September 02, 2008: Message edited by: Campbell Ritchie ]
Nitin Singla wrote:
Now also we are not overriding equals() method
Kaydell Leavitt wrote:marc's comments made me wonder whether my examples were correct or not so I verified that they are correct:
Hi, can you please explain me the equals() method overridden in the Integer class. It is given as follows:
public boolean equals(Object obj) {
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
}
Campbell Ritchie wrote:Welcome to the Ranch
![]()
You would appear to have used the quote tags wrongly; I have corrected them.
What nobody appears to have noticed in the method quoted, is that the instanceof operator can only be relied on in Integer because it is a final class. I don’t think Integer was a very good example, because it is final.
You need to cast the other reference to the same type as the class you are using; the tests preceding this cast ensure it will complete safely.
No, you are not comparing the locations of the references at all with the == operator. You are comparing the two ints in the objects.
Integer i = Integer.valueOf(123);
Integer j = Integer.valueOf(234);
System.out.println(i.equals(j)); // == operator tests 123 == 234
Campbell Ritchie wrote:You are still using the quote tags wrongly. Don’t delete [\quote]
You have not read the code quoted correctly. You are not comparing the references at all. You are comparing the two ints inside the Integer objects. What I would have written is something like this:-You can test whether they are the same object (line 3) which compares the references, or later (line 5) on test whether the two ints called value (the Integer class’ only field) are equal. The test whether the two references are the same allows you to short‑circuit the remainder of the equals() method. Remember if they are the same object, then equals must return true. Every object is equal to itself.
That equals method was probably written 18 years ago, and I would not use that as a paradigm for good coding. Remember instanceof works correctly in this case because the Integer class is final.
Campbell Ritchie wrote:You have misunderstood me, I am afraid.
Yes, you use the equals() method to compare objects for equality.
No, I was saying that there are neater ways to write an equals() method than that. I would never have used intValue() when I could have got access to the value field directly.
Tarun Oohri wrote:What is the need of using instanceof in equals method ? As every time it is going to give true value.
Mike Simmons wrote:
Tarun Oohri wrote:What is the need of using instanceof in equals method ? As every time it is going to give true value.
No, not necessarily. The signature says equals(Object), so the method needs to handle any possible Object, or null - not just an Integer.
This code should print false, twice. No exceptions thrown, no "true".
Tarun Oohri wrote: Oh ok got it...Thank you so much
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
ankush deshpande wrote:== compares if 2 references point to the same memory location
if this does not help i guess this link will solve your doubt. http://www.youtube.com/watch?v=7RG8HGIiuPY
Jeff Verdegan wrote:That video is rather confusing...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Isn’t it. It is full of errors.Winston Gutkowski wrote: . . . Forget that video. It's crap.
Winston
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |