Here we will learn about the precedence of Associativity of operators with the help of Example code.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
package jbt.operators; /* * Here we will learn about the different airthmetic operators in Java & how to use it. * Arithmetic Operators in Java * * 1: "+" Addition operators * 2: "-" Subtraction operators * 3: "*" Multiplication operators * 4: "/" Division operators * 5: "%" Modulus Operators (This operator divides left hand operand by right hand operand and returns the remainder) * */ public class ArithmeticOperator { public static void main(String[] args) { int i = 10; int j = 30; int k = 50; System.out.println("Addition Operators(i+j) : " + (i + j)); System.out.println("Subtraction Operators(i+j) : " + (i - j)); System.out.println("Multiplication Operators(i+j) : " + (i * j)); System.out.println("Division Operators(i+j) : " + (i / j)); System.out.println("Modulus Operators(i+j) : " + (i % j)); /* * Note*: If a calculation contains more then one arithmetic operator * then which will execute first is depend on the precedence & * associativity of operators(First Precedence then Associativity). * * If operation contains operators of Same precedence then associativity * will decide which will get executed first. * * Associativity of +/- is "Left to Right". Hence in below calculation * left side operator(i-j) will be executed first then right side * operator */ int l = i - j + k; System.out.println("Value of Calculation(i - j + k) : " + l); /* * "*" or "/" has better precedence over "+" or "-" * * So in below calculation first * operator will get executed as it has * precedence over + operator and then + will get executed. */ int m = i - j * k; System.out.println("Value of Calculation(i - j * k) : " + m); } } |