This week's book giveaway is in the Java in General forum.
We're giving away four copies of Java by Comparison (eBook) and have Simon Harrer, Jörg Lenhard, Linus Dietz on-line!
See this thread for details.
Win a copy of Java by Comparison (eBook) this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

equals( )  RSS feed

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please can you say me why operator overloading is not provided in java...where as c++ provided it.
If it is present,we can override == just like overriding the equals().


regards,
yasoda.
 
Sheriff
Posts: 9087
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is not provided because the creater of Java decided to not provide it. The '+' is overloaded (addition and String concatenation), but you cannot overload any operator.
 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because operator overloading makes code hard to mainten.

image if == was overload to be equivelent to Object.equals(Object x)

how would you mainten this piece of code:

if(someObject == someOtherObject)
{
//Do some random fancy code here
}

Reading that code how do you know if the condition is checking for equality of object reference or for equality of object?

You might be able to tell from the context of the code about the condition, but that isnt very robust.
I think it was a good call on the side of the designers.

G
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There will be more ambiguity if ther is concpt like operator overloading.

since java eliminates ambiguities that are present in c++ such as multiple inheritance ,operator overloading
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by yasoda aravapalli:
Please can you say me why operator overloading is not provided in java...where as c++ provided it.
If it is present,we can override == just like overriding the equals().


regards,
yasoda.



No operator overloading make JAVA simple. You always need to overriding the equals() , never use == to check Object.
 
Author and ninkuma
Marshal
Posts: 66480
160
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"zh ro", you have previously been warned on one or more occasions regarding adjusting your display name to meet JavaRanch standards. This is not optional. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it prior to your next post.

Your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Be aware that accounts with invalid display names are removed.

bear
JavaRanch Sheriff
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!