Solved

how to override operator in java?

Posted on 2001-08-01
9
6,913 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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
 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

734 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