-2

I'm trying to make a java library with a bunch of extra classes, and I was adding one for Imaginary numbers. Is there any way in java to make a custom class that is affected by mathematics operations for example

Imaginary(10) * Imaginary(50) = "500i"

1
  • 1
    Simple easy to Google answer: Java does not support operator overloading. Commented Jun 1, 2017 at 18:27

2 Answers 2

2

No, it is not possible to define custom arithmetic operators that overload default operators, but you can create methods like Imaginary.mutiply(Imaginary i).

Sign up to request clarification or add additional context in comments.

Comments

0

You cannot overload operators in Java. See this previous answer to a similar question: https://stackoverflow.com/a/5883909/1701316

Your class will need to implement its operations as methods. If you'd like, since any character is allowed in a method name, you can name them with the typical operators, but they'll still need to be called with dot-notation: Imaginary(10).*(Imaginary(50))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.