Solved

how to override operator in java?

Posted on 2001-08-01
9
6,907 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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 learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…

815 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

9 Experts available now in Live!

Get 1:1 Help Now