Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does mentioning multiple arithmetic operation within two operands work in Java

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?

like image 912
user2381832 Avatar asked Jul 16 '15 13:07

user2381832


People also ask

What are arithmetic operators in Java?

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.

Which operator is used to add or subtract two operands?

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.

What is the difference between operators and operands in Java?

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 −

What is the use of addition operator in Java?

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.


1 Answers

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 +

like image 56
Madhawa Priyashantha Avatar answered Nov 10 '22 17:11

Madhawa Priyashantha