examples of operators priorities in java

1

The priority is in order as follows:

 

1. ^ power

2. Increase by one++ increment and decrease by one -- decrement

3. Remainder %

4. Multiplication and division * /

5. multiplication and subtraction

6. Positive + and negative signs - unary plus and minus operators

7. + - add and subtract

8. = assignment

All Contributions

example of priorities in java:

int n1=5;
int n2=4;
int n3=7;
int n4=12;
int result=n1+n2+n3+n4;   //first n1+n2, then its result + n3, then its result + n4
System.out.println("n1+n2+n3+n4 = "+result);
result=n1+n2*n3-n4; //first n2*n3, then its result + n1, then its result -n4
System.out.println("n1+n2*n3-n4 = "+result);
result=(n1+n2)*(n3-n4); //first n1+n2, then n3-n4, then finaly multiply the 2 results with each other
System.out.println("(n1+n2)*(n3-n4) = "+result);

total contributions (1)

New to examplegeek.com?

Join us