• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

about Equals

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess the answer given is wrong. I tried the code and I got the answer as true in both the cases.
 
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi ,
but lancy when i tried this code. i got the answer as
false
true

pls. somebody help us out.
regards
deekasha

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Ravindra:
the code is giving
true
true
as the output
i am using jdk 1.2.2
regards,
Smita
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I tried the code and its giving the output as expected
true
true
Thanks
Sanjeet
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried this code and got output as
true
true
I am using java 1.2.2
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got
false
true
I'm using JDK 1.2.1
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi ravi ...
the problem is u r giving is logically imposssible ...
the answer is wrong ...
thanks ...
manal ....
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

my ouput is
false
false
surprise,
why?
mick
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I tried this out but the answer for me is
False
True
Why is the difference !
Can anybody explain ! Thanx in advance
Ravi
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, I tried and it gives random out put.
According to my knowledge it should print "true in both the cases"
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..


 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic