10-23-2012 09:40 AM
Last question Sir
String str1 ="abc"
String str2 ="abc"
you want to say vM store this as a same object . that why str1==str2 print
String str1 ="ab"
String str2 ="abc"
you want to say vM store this as a different object . that why str1==str2 not print
10-23-2012 12:51 PM - edited 10-23-2012 12:58 PM
When you assign the same value into both string. then its hascode will be same.
Like if, you put.
Here for Str1, Str2 hashCode is same. That is why it prints "Equal".
And, once you change the content of the string, then their hashCode is changed. So it does not print "Equal".
You may check hasCode in your code also.
In other word, you can also say that hasCode is generate on the behalf of content of this variable.
And, when the hascode will not match, their reference become different.
Thats why this statement does not print "Equal".
String str1 ="ab";
String str2 ="abc";
if(str1 == str2)
10-23-2012 09:55 PM
Pawan Thanks for reply.
String str2 ="test"
when i print the hashcode they are same ..you are right.
String abc =new String("test")
but when i print abc hash code it also print the same value .but why abc==str1 not printing..?
10-24-2012 12:17 AM
As you know that
String str1="test", this is reference not an object. and
String abc =new String("test") this is object, so when you try to compare this like... str1==abc, this will not be true. and not print the "Equal".
Even you may try it like this also.
String abc1 = new String("A");
String abc = new String("A");
if(abc == abc1)
Here it will print "Not Equal".
As earlier @simon told you: str1==str2 will never succeed as the '==' operator compares the reference to the object. equals compares the object itself, so you need to use here equal method.
10-24-2012 12:59 AM
i understand every thing but need some confusion about what is reference actually i know object ..
how i know which is reference ..?
String str1="test", this is reference .
from my point of view str1 is also an object we consider as literal . b
please give some little bit explaination about reference ?
10-24-2012 01:03 AM