| | |
kindly explain this strange behaviour of == and inerface
![]() |
•
•
Join Date: Jun 2008
Posts: 14
Reputation:
Solved Threads: 1
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 false.can you tell me why?
2. here goes the second observation
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
These are as follows:-
1. here is the first observation
The output of this is false but not
Java Syntax (Toggle Plain Text)
str1 == str2
Java Syntax (Toggle Plain Text)
String str1 = "shobhit"; String str2 = new String("shobhit"); System.out.println("str1 == str2 " + str1 == str2);
2. here goes the second observation
Java Syntax (Toggle Plain Text)
interface abc { public boolean equals(java.lang.Object arg0); public void x(); public String toString(); } public class xyz implements abc{ public void x() { // TODO Auto-generated method stub } }
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
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).
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
----------------------------------------------
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
•
•
Join Date: Jun 2008
Posts: 14
Reputation:
Solved Threads: 1
•
•
•
•
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).
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.
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
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
----------------------------------------------
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
![]() |
Other Threads in the Java Forum
- Previous Thread: Count Odd Numbers
- Next Thread: doubt on run method
| Thread Tools | Search this Thread |
account android api applet application array arrays automation bidirectional binary birt bluetooth class classes client code columns component constructor database designadrawingapplicationusingjavajslider draw eclipse error errors exception expand fractal game givemetehcodez graphics gui guidancer homework html ide image inetaddress inheritance integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jlabel jme jni jpanel jtextfield jtree julia linux list loop map method methods midlethttpconnection mobile mobiledevelopmentcreatejar monitoring myaggfun netbeans newbie nullpointerexception open-source oracle plazmic print problem program project property recursion ria scanner search server set sharepoint smart sms smsspam sort sourcelabs splash sql sqlite static string subclass support swing testautomation threads tree unlimited webservices windows






