I have an expression in my code - int i = 10 + + 11 - - 12 + + 13 - - 14 + + 15;
The value of the variable 'i' evaluates to 75, which is the sum of all the integers mentioned in the expression. How does the evaluation happen in this scenario?
These operators consist of various unary and binary operators that can be applied on a single or two operands. Let’s look at the various operators that Java has to provide under the arithmetic operators. Now let’s look at each one of the arithmetic operators in Java: 1.
1. Addition (+): This operator is a binary operator and is used to add two operands. 2. Subtraction (-): This operator is a binary operator and is used to subtract two operands.
Operators are like + , - whereas operands are values on which operates. The following program is a simple example which demonstrates the arithmetic operators. Copy and paste the following Java program in Test.java file, and compile and run this program −
For example expressions 5+10; 5 + 10; 5 + 10; are same and will run without any error. Each operand in an arithmetic operation can be a constant value, a variable/expression, a function call returning a compatible value or a combination of all these. The addition (+) operator in java is also used for string concatenation.
this evaluate as
int i = 10 + (+ 11) - (- 12) + (+ 13) - (- 14) + (+ 15);
evaluate to
int i= 10 +11+12+13+14+15;
and all become + so value is 75.note - -
is +
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With