Jump to content
  • Advertisement
Sign in to follow this  
Bojevnik

[java] How to use = on custom classes?

This topic is 3905 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement

if ( a.equals(b) ) {};

// Comparable
if ( 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.

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!