Solved

how to override operator in java?

Posted on 2001-08-01
9
6,906 Views
Last Modified: 2008-01-09
hi, I'm a C++ program and new in Java, how can I override the operators (==,+=,=,-= etc) in Java ? For example in c++ we do :

Class Test
{
   int value;
   Test()
   {
     value= 0;
   };
   
  const Test& operator =(Test t)
  {
    this->value = t.value;
  }
  bool operator ==(Test t)
  {
    return this->value == t.value;
  }
}

how to do this in Java?
0
Comment
Question by:eugeneng
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 1

Accepted Solution

by:
LDC earned 30 total points
ID: 6343535
You cannot overload operators in java.
The = operator is useless anyway because it is always pointers/references that you handle.
The == should not be used, but the method equals() should be. (== tests whether the "pointers" are the same).
0
 
LVL 5

Expert Comment

by:LexZEUS
ID: 6343641
use equals() to compare 2 objects, or maybe hashCode() ...

For easier apporach, 2 objects are same if :
1. Have same parent class / interface
2. The hashCode() are same
0
 
LVL 7

Expert Comment

by:tomboshell
ID: 6343716
If you need a function that 'acts' like an overloaded operator than it would be best to write your own.  You did in the example.  You can override many functions, creating your own implementation if needed.  Or write a similar function.  Many programmers write their own utilties libraries of functions they need.  

Tom
0
 
LVL 1

Expert Comment

by:LDC
ID: 6344221
LexZEUS, 2 objects are not necessarily the same if the hasCode() is the same, even for a given class. The only specification in hashCode is that it returns the same number for equal objects, not that it return different numbers for different objects.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:eugeneng
ID: 6346978
so is operator overriding "achievable" in java or not ?
0
 
LVL 5

Expert Comment

by:gbentley
ID: 6347100
The simple answer is no. Java does not support operator overloading. It is by design. It does mean a different sty

Regards
Gordon
0
 
LVL 5

Expert Comment

by:gbentley
ID: 6347101
The simple answer is no. Java does not support operator overloading. It is by design. It does mean a different style of object design is needed.

Regards
Gordon
0
 
LVL 5

Expert Comment

by:gbentley
ID: 6347102
Sorry about the double message!
0
 
LVL 5

Expert Comment

by:LexZEUS
ID: 6347142
>LexZEUS, 2 objects are not necessarily the same if the hasCode() is the same, even for a given class.
The only specification in hashCode is that it returns the same number for equal objects, not that it return different numbers for different objects.

Yes... I just said the previous comment for a basic implementation of using equals() method for generic Class ...

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now