?
Solved

how to override operator in java?

Posted on 2001-08-01
9
Medium Priority
?
6,920 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 120 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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month9 days, 20 hours left to enroll

762 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