“+” operator for Java-classes

后端 未结 5 1845
日久生厌
日久生厌 2020-12-17 16:24

I have a class like this:

private static class Num {
    private int val;

    public Num(int val) {
        this.val = val;
    }
}

Is it

5条回答
  •  忘掉有多难
    2020-12-17 17:03

    There is no operators overloading in java. The only thing which is supported for objects, is string concatenations via "+". If you have a sequences of objects joined via "+" and at least one of them is a String, then the result will be inlined to String creation. Example:

    Integer a = 5;
    Object b = new Object();
    
    String str = "Test" + a + b;
    

    will be inlined to

    String str = new StringBuilder("Test").append(a).append(b).toString();
    

提交回复
热议问题