This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Why it prints like this here?  RSS feed

 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers,

Cosider the followinf code as below,

string str = "Java";
StringBuffer sb = new StringBuffer(str);
if (str.equals(sb)){
System.out.println("Both are equal");
}else{
System.out.println("Both are not equal");
}

Why here it prints Both are not equal??

Regards,
Jothi Shankar Kumar. S
 
toukhir mujawar
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rather than your code try this one...

String str = "Java";
StringBuffer sb = new StringBuffer();
sb.append(str);
if (str.equals(sb)){
System.out.println("Both are equal");
}else{
System.out.println("Both are not equal");
}


this should work....
 
Alina Petra
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look at the overridden equals() method within the String class you see that it checks if the object given as parameter is an instance of String. If it's not, it retuns false. And since you compare your String object with a StringBuffer object you get false.
 
toukhir mujawar
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that's nice Alina Petra

we should user
sb.toString() before comparing...


thanks for your analysis...
 
Rancy Chadha
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jothi,

The reason you got output as "Both are not equal" is you are comparing objects of different type.
str is object of String class and sb is object of StringBuffer class
When JVM finds comparison made between objects of two different classes, it invokes equals() method of Object class
which thereby checks only if two variable are pointing to same object or not.

Hope this clears the reason why you got that output.

Regards,
Rancy
 
yogesh sood
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When JVM finds comparison made between objects of two different classes, it invokes equals() method of Object class


I think this might be not true, and description provided by "Alina" seems to be correct.

Can anyone else please comment on this or "Rancy" can you provide some more description or refer to resource where you read this.

As i think it will be calling overriden method of String.
 
gopinathang nathan
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sb.append() also does not seem to be working.

if (str.equals(sb.toString)) worked for me and makes sense.
 
Shyam kumar
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rancy Chadha is correct. String class overrides the .equals method. Inside the equals method the String class makes the following comparison:

public boolean equals(Object anObject) {

if (!(anObject instanceof String)) {
return false;
}
-----
Since stringbuffer object dont have a subclass/superclass relation with string class, false is returned.
 
yogesh sood
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Naveen,

Thanks for your input. Please check the text from "Rancy" i have quoted in my post. So, you also agree that it doesnt call equals() method of Object class which is contrary to what "Rancy" mentioned.
 
Rancy Chadha
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Yogesh,

First of all apologies for late reply.
What I said was that

WHEN JVM FINDS COMPARISON MADE BETWEEN TWO OBJECTS BELONGING TO TWO DIFFERENT TYPES IT INVOKES equals() method of Object class.

Reason why I said it is:

Every Object that is created is a derived from Object class.
Now in this case we have equals() method overriden in String class.
So what JVM does on encountering str.equals(sb) statement, it calls equals() method of String class. But since the object we passed as parameter is not a string object so what does it do, it calls equals() method of Object class.

There are two scenarios we can think of, when we see statement of this kind
"objA.equals(objB)", Constraints: objA and objB belong to two different classes:

1) If objA's class has an equals method then in this case it will invoke its own equals() method and it will find objB is not of its own class type. Since it is not it will invoke equals() method of Object class.
2) If objA's class doesnot at all have equals() method then certainly it will call the equals() method of Object class.

I hope this clears why I gave that statement. May be I was wrong in framing it. But explanation
I provided above is what I actually meant.

Thanks,
Rancy
 
yogesh sood
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rancy,

Seems like we are still on not same page on your comment which i quoted below.

1) If objA's class has an equals method then in this case it will invoke its own equals() method and it will find objB is not of its own class type. Since it is not it will invoke equals() method of Object class.

When we override our and provide implementation for equals() we can do this, but in String class defined in java.lang this is not the case. Please check code below from Java library as you can see from code below that there is no call to Object.equals() method

 
Rancy Chadha
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Yogesh,

I got it now.
It was my mistake.
Thanks for making clear.

Regards,
Rancy
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!