my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes about Equals Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "about Equals" Watch "about Equals" New topic
Author

about Equals

Ravindra
Greenhorn

Joined: Jul 08, 2000
Posts: 20
hi friends,
this question is from a mock exam :
what is the output of the following program ?
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));
System.out.println(b.equals(c));
}
}
Output given is :
false
true
but how can it be possible that a.equals(b) gives "false", and on the same time b.equals(c) gives "true".
please help me about this.
thanks in advance.
Ravi..

[This message has been edited by Ravindra (edited August 18, 2000).]
Lancy Mendonca
Ranch Hand

Joined: Aug 08, 2000
Posts: 54
I guess the answer given is wrong. I tried the code and I got the answer as true in both the cases.


Sun Certified Java Programmer<BR>Oracle Certified DBA
deekasha gunwant
Ranch Hand

Joined: May 06, 2000
Posts: 396
hi ,
but lancy when i tried this code. i got the answer as
false
true

pls. somebody help us out.
regards
deekasha

Smita Jain
Greenhorn

Joined: Aug 16, 2000
Posts: 8
hi Ravindra:
the code is giving
true
true
as the output
i am using jdk 1.2.2
regards,
Smita
Sanjeet Karamchandani
Ranch Hand

Joined: Jul 29, 2000
Posts: 30
Hi,
I tried the code and its giving the output as expected
true
true
Thanks
Sanjeet
sunita singh
Greenhorn

Joined: Aug 03, 2000
Posts: 12
I tried this code and got output as
true
true
I am using java 1.2.2
Bill Howerton
Greenhorn

Joined: Aug 18, 2000
Posts: 7
I got
false
true
I'm using JDK 1.2.1
manal
Ranch Hand

Joined: Aug 17, 2000
Posts: 45
hi ravi ...
the problem is u r giving is logically imposssible ...
the answer is wrong ...
thanks ...
manal ....
Harry Chawla
Ranch Hand

Joined: Jun 03, 2000
Posts: 97
Ha...It got u guys too...
My dear Friends
In case of any doubt, pls try to use the elaborate and excellent search facility of JAVARANCH.
Pls refer to the following link for explanation. http://www.javaranch.com/ubb/Forum24/HTML/002661.html
Thanks.
[This message has been edited by Harry Chawla (edited August 18, 2000).]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944

my ouput is
false
false
surprise,
why?
mick
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Originally posted by Harry Chawla:
[B]Ha...It got u guys too...
My dear Friends
In case of any doubt, pls try to use the elaborate and excellent search facility of JAVARANCH.
Pls refer to the following link for explanation. http://www.javaranch.com/ubb/Forum24/HTML/002661.html
Thanks.
Thanks,
It is much clear now, so this question is wrong not the answer
Mick
Ravindra
Greenhorn

Joined: Jul 08, 2000
Posts: 20
Thanks Harry,
but I still have a doubt.
does the real exam contains such type of questions ?
thanks again for all to reply.
and manal,
if you know MCW, then pls contact me at : ravindra_jain12@rediffmail.com
bye...
Raghavendra Holla
Ranch Hand

Joined: Jun 02, 2000
Posts: 58
The answer given may be wrong as well as right. This is because according to JAVA doc GregorianCalendar() will return System date and time. Then equals depends on object creation date and time. You can try same code by printing all objects viz. a,b,c.
Regards,
R V Holla.
Ravi Chandiran
Greenhorn

Joined: Dec 11, 2000
Posts: 2
Hi, I tried this out but the answer for me is
False
True
Why is the difference !
Can anybody explain ! Thanx in advance
Ravi
Tejas Kansara
Ranch Hand

Joined: Dec 14, 2000
Posts: 38
Actually, I tried and it gives random out put.
According to my knowledge it should print "true in both the cases"
Ravindra Jain
Greenhorn

Joined: Dec 18, 2000
Posts: 1
Hi Ravi & Tejas,
pls refer above given links to find out why is the difference.
(http://www.javaranch.com/ubb/Forum24/HTML/002661.html)
I'm giving the part of the answer (thanks to Carl):
by Carl Trusiak :
"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. "
now I hope the answer will be very clear to both of u.
good luck for both of u.
thanks,
Ravindra..
saumil shukla
Ranch Hand

Joined: Dec 01, 2000
Posts: 47
did anybody said that Java is platform independent?
How funny!!!
Originally posted by Ravindra Jain:
Hi Ravi & Tejas,
pls refer above given links to find out why is the difference.
(http://www.javaranch.com/ubb/Forum24/HTML/002661.html)
I'm giving the part of the answer (thanks to Carl):
by Carl Trusiak :
"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. "
now I hope the answer will be very clear to both of u.
good luck for both of u.
thanks,
Ravindra..

Hemant Patel
Greenhorn

Joined: Dec 15, 2000
Posts: 20
Hi all
I tried this code it gives output
true
true
i don't know why this same code difference output to other body. i don't know calender so i haven't idea about this which one is correct
thnks,
Himanshu Amin
------------------
"JAVA BANAYE BAVA"
Faisal Naveed
Greenhorn

Joined: Dec 18, 2000
Posts: 1
Aoa,
Dear Try to run the program again, sometimes you will get the answer true,true and sometimes there will be undesired results like the one you got False,True.
This is due to the CPU used by the JVM, since GregorianCalendar() will return System date and time, so there comes the difference in time in micro micro seconds, that sometimes causes for such type of results.
Hopefully this will help you out.
Regards,
Faisal Naveed

Originally posted by Ravi Chandiran:
Hi, I tried this out but the answer for me is
False
True
Why is the difference !
Can anybody explain ! Thanx in advance
Ravi


------------------
Regards,
Faisal Naveed


Regards,<BR> Faisal Naveed
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: about Equals