[java] How to use = on custom classes?

Started by
3 comments, last by CaptainJester 16 years, 6 months ago
Is it possible to use <>= on custom class? for example ... CustomClass a = new CustomClass(x); CustomClass b = new CustomClass(y); if(a<b)... I already played with comparable and comparator interface but with no luck.
Fear is the mindkiller. Fear is the little-death that brings total obliterationI will not fear. I will permit it to pass over me and through me.And when its has gone past I will turn the inner eye to see it's path.Where fear has gone, there will be nothing, only i will remain.
Advertisement
if ( a.equals(b) ) {};// Comparableif ( a.compareTo(b) < 0 ) {}


Relational operators

Note that you need to override and implement both of those methods according to their JavaDoc. That includes the hashCode() method.
Quote:Original post by Bojevnik
Is it possible to use <>= on custom class?
for example

...
CustomClass a = new CustomClass(x);
CustomClass b = new CustomClass(y);
if(a<b)...


I already played with comparable and comparator interface but with no luck.


using Java, AFAIK No.

you can hower create methods that does the comparison for you.
(Look at how the String class works for example)
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
Thank you both.

I knew about other methods, but this way just seemed faster to write(especialy by hand), too bad i cant be done.
Fear is the mindkiller. Fear is the little-death that brings total obliterationI will not fear. I will permit it to pass over me and through me.And when its has gone past I will turn the inner eye to see it's path.Where fear has gone, there will be nothing, only i will remain.
You can't do it in C++ by default either. You still have to write code to do it. You just get to write the code to use the mathematical signs instead of method calls.
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]

This topic is closed to new replies.

Advertisement