GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes equals( ) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "equals( )" Watch "equals( )" New topic
Author

equals( )

Ar Yasoda
Greenhorn

Joined: Apr 04, 2007
Posts: 9
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.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
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.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
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
bala nannaka
Ranch Hand

Joined: Apr 02, 2007
Posts: 49
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
Zheng Ron
Greenhorn

Joined: Apr 05, 2007
Posts: 3
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.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61316
    
  66

"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


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
GeeCON Prague 2014
 
subject: equals( )