Solved

how to override operator in java?

Posted on 2001-08-01
9
6,905 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
network + 7 73
wordlen challenge 3 49
Error with Java/Cache JDBC Classpath 2 21
ArrayIndexOutOfBoundException 9 39
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…
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 …
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

747 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

11 Experts available now in Live!

Get 1:1 Help Now