Hi all I have a doubt on equals() method.import java.util.*; class Equals { public static void main(String [] args) { Calendar a = new GregorianCalendar(); Calendar b = new GregorianCalendar(); Calendar c = new GregorianCalendar(); System.out.println("a.equals(b)?" + a.equals(b)); System.out.println("a == b)?" + (a == b)); System.out.println("b.equals(c)?" + b.equals(c)); } } when we run the code, we get the following: a.equals(b)? false a == b? false b.equals(c)? true Can please some one explain me why a.equals(b) returns false, and b.equals(c) returns true. Thank you in advance Rani.
Balki Dhar
Greenhorn
Joined: Dec 15, 2005
Posts: 18
posted
0
Hi Rani The output of your example should be: a.equals(b)?true a == b)?false b.equals(c)?true The API documentation for Calendar class says: equals(): Compares this calendar to the specified object. The result is true if and only if the argument is not null and is a Calendar object that represents the same calendar as this object. The Calendar class overrides equals method in such a way that if the contents of the two date objects are equal, it will return true. when an object is created using 'new' java allocates memory for the new object and initialized it to default values. So a and b both will have default values and equals method returns true. a==b, compares the memory location. since the date objects are created new, java allocates separate memory location for the each new objects and this memeory location is different for each new object. so a==b returns false since it is not comparing the contents but physical memory location address. Balki
Rani Masanam
Greenhorn
Joined: Jul 23, 2000
Posts: 3
posted
0
Hi Balki Thanks for your help. But When I run the code, I got a.equals(b)? false Can you please check the code on computer? May be I was wrong. Thank You.
Rani Masanam
Greenhorn
Joined: Jul 23, 2000
Posts: 3
posted
0
Hi Balki Thank you for your help. Did you try the code on computer? But I am still getting the output as a.equals(b)? false Can you please try it on computer and reply me back. may be I was wrong. Thank you
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
I've Various Equal Test Examples in a file "TestEqual.java" at http://members.spree.com/education/javachina/Cert/TestEqual.html It pretty much covers most situation encountered with "equal()" or "==". Some of them are pretty tricky. Check it out! Warning: Do not try this at work! Roseanne
Balki Dhar
Greenhorn
Joined: Dec 15, 2005
Posts: 18
posted
0
Hi Rani I just did cut & Paste of your code and ran on my machine. It gave a.equals(b) true. Balki
RAHUL HINGE
Greenhorn
Joined: Jul 11, 2000
Posts: 1
posted
0
Hi Rani I just did cut & Paste of your code and ran on my machine. It gave a.equals(b) true. Rahul
Praveen Zala
Ranch Hand
Joined: Jul 02, 2000
Posts: 118
posted
0
Hi Rani, U r right abt the code - it gave me a false for the first case i.e. a.equals(b)? Actually if evryone is wondering what JDK and platform I am using - it is JDK1.2.1 and WIN 95........ Still I am also foxed why this is happening ? Can MahaAnna look into this ? Praveen Zala
pollai mandala
Greenhorn
Joined: Jun 16, 2000
Posts: 17
posted
0
Hi ALL , I too tried that example.Out put is: a.equals(b)?true a == b)?false b.equals(c)?true I am using: java version "1.2.2" Classic VM (build JDK-1.2.2-W, native threads, symcjit) It is interesting,any way , regards
Regards,<BR>Bhavani
latha
Greenhorn
Joined: Jul 17, 2002
Posts: 14
posted
0
Hi All!! I also tried this example.My output is a.equals(b)?true a == b)? false b.equals(c)?true I am using JDK 1.2.1 on WIN98
I couldn't understand why it's giving different outputs on different JDK versions??? let me know pls...
Carl Trusiak
Sheriff
Joined: Jun 13, 2000
Posts: 3340
posted
0
Hello all, Correct output from this program could be a.equals(b)? false or a.equals(b)? true The answer is defined in what equals() does in the Calendar class. It does a comparision of the Date and Time of the Calendar Object to 1/100 of a second. The faster the machine is that you are running on, the more time you'll get true as a response. The slower, the more times you'll get false. Fast CPU's can perform many operations in 1/100 of a second including the creation of Calendar Objects. On my PC, it returns true for a.equals(b)? true and for b.equals(c)? true and also for a.equals(c)? true This is because the three objects where created all within that 1/100 of a second. Try creating 10 of them and see how many your PC can create on average in 1/100 of a second.