Win a copy of Mastering Corda: Blockchain for Java Developers this week in the Cloud/Virtualization forum!

Lakhdeep Singh

Greenhorn
+ Follow
since Feb 01, 2006
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Lakhdeep Singh

firstly i want to tell u what r abstract methods--which have no body.
Basically we use abstract methods to hide the implementation from the classes which have their prototypes. Overriding of abstract method is compulsary if u dont want 2 make sub class as an abstract & want to give the body 2 abstract method , otherwise it's not necessary at all.

Thank you
Hi, its amazing question. Here i want to clear that equals method basically used with the instances from same class. if we use with different instances it will return false.
As u have taken s as a instance of String & i as an instance of integer
both are peer classes & inherited from object class & both have their independant implementation of "boolean equals(Object)" method .

Implementation of equals in Integer:

public boolean equals(Object obj) {
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
}


Implementation of equals in String:

public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = count;
if (n == anotherString.count) {
char v1[] = value;
char v2[] = anotherString.value;
int i = offset;
int j = anotherString.offset;
while (n-- != 0) {
if (v1[i++] != v2[j++])
return false;
}
return true;
}
}
return false;
}


from both implementation of equals u kan check both use the statement
"anObject instanceof <ClassName>" if invoking instance & parameter are the instances of different types then it will return false.

Thanks.
Lakhdeep
"Java & Only Java Have The Power Which Can Change The World"
It is due to + operator, in java it is implecitly overloaded.
& precedence & association of operators applied here