kindly explain this strange behaviour of == and inerface

Reply

Join Date: Jun 2008
Posts: 14
Reputation: shobhit123 is an unknown quantity at this point 
Solved Threads: 1
shobhit123 shobhit123 is offline Offline
Newbie Poster

kindly explain this strange behaviour of == and inerface

 
0
  #1
Sep 3rd, 2008
I had come across some strange behaviour while i was looking at some java code.
These are as follows:-

1. here is the first observation
The output of this is false but not
  1. str1 == str2
false.can you tell me why?
  1.  
  2. String str1 = "shobhit";
  3. String str2 = new String("shobhit");
  4. System.out.println("str1 == str2 " + str1 == str2);


2. here goes the second observation
  1. interface abc {
  2. public boolean equals(java.lang.Object arg0);
  3. public void x();
  4. public String toString();
  5. }
  6.  
  7. public class xyz implements abc{
  8.  
  9. public void x() {
  10. // TODO Auto-generated method stub
  11.  
  12. }
  13.  
  14. }
here interface abc has some object class methods.now class xyz has implemented abc and has not implemented the equals and toString methods.still the compiler does not give error but why?
what is the relation b/w interface and object class.
I found this observation when i tried to implement Comparator interface and did not implement equals method and still did not get any compilation errors.
Can you kindly expalin the reasons for the same.

Thanks in advance
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,331
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 249
Moderator
masijade's Avatar
masijade masijade is online now Online
Nearly a Posting Maven

Re: kindly explain this strange behaviour of == and inerface

 
0
  #2
Sep 3rd, 2008
equals and toString exist in the Object class (which all Classes implicitly extend), so the class does implement them.

Also, == , when it comes to objects, compares there reference values (i.e. are they the same actual object). It does not compare there content (i.e. do they both contain the value "shobhit". If you wish to compare the content, use str1.equals(str2).
Last edited by masijade; Sep 3rd, 2008 at 2:42 am.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 14
Reputation: shobhit123 is an unknown quantity at this point 
Solved Threads: 1
shobhit123 shobhit123 is offline Offline
Newbie Poster

Re: kindly explain this strange behaviour of == and inerface

 
0
  #3
Sep 3rd, 2008
Originally Posted by masijade View Post
equals and toString exist in the Object class (which all Classes implicitly extend), so the class does implement them.

Also, == , when it comes to objects, compares there reference values (i.e. are they the same actual object). It does not compare there content (i.e. do they both contain the value "shobhit". If you wish to compare the content, use str1.equals(str2).
Thanks for replying,
But another question which comes to my mind is then why the Comparator interface has been provided with equals() method.if anyways every class has got it from Object class and it can override it in the way it want then why it has been provided in Comaparator interfcae.ven if we dont impplement the equals mehtod , it is alwasy there.Pls correct me if i am wrongg.

Also
Thanks for showing concerc to my question but
i am not asking that why it resturned false
i am asking that the following code returned me only 'false' and not 'str1 == str2 false'. can you kindly explain.
String str1 = "shobhit";
String str2 = new String("shobhit");
System.out.println("str1 == str2 " + str1 == str2);

Also if i say this
System.out.println("str1 == str2 " + str1.equals(str2));

then i get the output as 'str1 == str2 true' and not just 'true'.

similarly i shud have got 'str1 == str2 false' and not just 'false'.

Kindly explain.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,331
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 249
Moderator
masijade's Avatar
masijade masijade is online now Online
Nearly a Posting Maven

Re: kindly explain this strange behaviour of == and inerface

 
0
  #4
Sep 3rd, 2008
Because "str1 == str2 " is a String that you are printing in that statement.

As far as Comparator goes, read this http://java.sun.com/javase/6/docs/ap...omparator.html
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC