12

I am attempting to create a unsigned integer class.

public class UnsignedInteger extends Number implements Comparable<UnsignedInteger> 
    { 
    ... 
    }

Is there a way to implement operators such as; +, -, *, /, <<, >>, |, ^, >>>, <<

2
  • 4
    Java does not support overloading operators! C# does~ ;) Commented Feb 12, 2013 at 14:16
  • 2
    I guess i was missing the forest while looking at the trees. Commented Feb 12, 2013 at 14:43

7 Answers 7

24

Java does not support Operator Overloading. The only option you have is define methods like add(), subtract(), multiply(), etc, and write the logic there, and invoke them for particular operation.

You can have a look at BigInteger class to get an idea of how you can define methods to support various operations. And if interested, you can even go through the source code, that you can find in the src folder of your jdk home directory.

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

2 Comments

This is a good answer, I think that some explanation on why operator overloading was discluded from the language would add a lot to it :)
@BenjaminGruenbaum. I have added a link to another post on SO, which describes that.
10

There are already 5 answers saying that you cannot overload operators, but I want to point out that you can not use arithmetical operators on objects at all. They only work with primitive types (int, double, etc).

The only reason the following code compiles

Integer a = 1, b = 2;
Integer c = a + b;

is because the Java compiler compiles it as

Integer a = Integer.valueOf(1), b = Integer.valueOf(2);
Integer c = Integer.valueOf(a.intValue() + b.intValue());

If you want this to work for your UnsignedInteger, you have to extend the javac (it is possible, though).

1 Comment

I give a like for the funny ending. That is a good programmer´s joke. Oh java does have String operations
4

No you cannot override operators in Java.

Comments

4

It's not possible to override operators in Java. What you can do is define methods to represent the operations, like BigDecimal or BigInteger in the standard library do.

Comments

3

There is a javac-plugin (an annotation processor like Lombok) called "Java-OO", which adds operator overloading to Java.

It allows you to add operator overloading to your own classes very easily. In addition to this, many of the built-in classes of the Java API also supports operator overloading when using this plugin. (For example: Instead of list.get(6) or map.get("hello") you can do list[6] and map["hello"])

All you need to do is to include the .jar on the classpath when compiling with javac.

There are plugins for all major IDEs: Eclipse, Netbeans and IntelliJ IDEA.

Comments

2

No. Java does not support operator overloading.

Comments

2

Java doesnt support operator overloading, they consider it a bad practice, knowing that they overloaded + and += operators for the String class

1 Comment

That's only half true. It's syntactic sugar for strings and converted into StringBuilder#append() calls by the javac.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.