Operator overloading in Java

前端 未结 10 1746
独厮守ぢ
独厮守ぢ 2020-11-21 23:51

Please can you tell me if it is possible to overload operators in Java? If it is used anywhere in Java could you please tell me about it.

相关标签:
10条回答
  • 2020-11-22 00:24

    As many others have answered: Java doesn't support user-defined operator overloading.

    Maybe this is off-topic, but I want to comment on some things I read in some answers.

    About readability.
    Compare:

    1. c = a + b
    2. c = a.add(b)

    Look again!
    Which one is more readable?

    A programming language that allows the creation of user-defined types, should allow them to act in the same way as the built-in types (or primitive types).

    So Java breaks a fundamental principle of Generic Programming:
    We should be able to interchange objects of built-in types with objects of user-defined types.
    (You may be wondering: "Did he say 'objects of built-in'?". Yes, see here.)

    About String concatenation:

    Mathematicians use the symbol + for commutative operations on sets.
    So we can be sure that a + b = b + a.
    String concatenation (in most programming languages) doesn't respect this common mathematical notation.

    a := "hello";
    b := "world";
    c := (a + b = b + a);
    

    or in Java:

    String a = "hello";
    String b = "world";
    boolean c = (a + b).equals(b + a);
    

    Extra:
    Notice how in Java equality and identity are confused. The == (equality) symbol means:
    a. Equality for primitive types.
    b. Identity-check for user-defined types, therefore, we are forced to use the function equals() for equality.
    But... What has this to do with operator overloading?
    If the language allows the operator overloading the user could give the proper meaning to the equality operator.

    0 讨论(0)
  • 2020-11-22 00:25

    Operator overloading is used in Java for the concatenation of the String type:

    String concat = "one" + "two";
    

    However, you cannot define your own operator overloads.

    0 讨论(0)
  • 2020-11-22 00:25

    You can't do this yourself since Java doesn't permit operator overloading.

    With one exception, however. + and += are overloaded for String objects.

    0 讨论(0)
  • 2020-11-22 00:27

    In addition to all the people pointing out that + is overloaded for Strings, - is also overloaded for both floating point and integer operations, as are * and /.

    [edit] % is also overloaded for floating point, which can be a bit of a surprise for those with a C or C++ background.

    0 讨论(0)
  • One can try Java Operator Overloading. It has its own limitations, but it worth trying if you really want to use operator overloading.

    0 讨论(0)
  • 2020-11-22 00:33

    Just use Xtend along with your Java code. It supports Operator Overloading:

        package com.example;
    
    @SuppressWarnings("all")
    public class Test {
      protected int wrapped;
    
      public Test(final int value) {
        this.wrapped = value;
      }
    
      public int operator_plus(final Test e2) {
        return (this.wrapped + e2.wrapped);
      }
    }
    
    package com.example
    
    class Test2 {
    
        new() {
            val t1 = new Test(3)
            val t2 = new Test(5)
            val t3 = t1 + t2
        }
    
    }
    

    On the official website, there is a list of the methods to implement for each operator !

    0 讨论(0)
提交回复
热议问题