Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Java, the expression "+ + + + + +" is executed, the compiler reports no errors and can execute correctly? [duplicate]

Tags:

java

I was writing this code in eclipse, it war written, and the result is 3d.

public static void main(String[] args) {
    double a = 5d + + + + + +-+3d;
    System.out.println(a);
}
like image 736
itjun Avatar asked Dec 23 '22 19:12

itjun


1 Answers

Your expression can be rewritten as

(5d) + (+ + + + +-+3d)

Where the first + is the addition operator applied to two operands.

All the + and - in + + + + +-+3d are unary operators that add up to the sign of the number 3d.

In the end, your arithmetic expression is

5d + (-3d)

Which returns 2d. You can apply multiple unary operators to an expression, as in the following examples:

+ - - 2 // 2
- + + 2 // -2
like image 66
ernest_k Avatar answered Feb 15 '23 22:02

ernest_k