Home / Programs / precedence in operators example in java
🚀 Programming Example

precedence in operators example in java

👁 1,493 Views
💻 Practical Program
📘 Step Learning
precedence in operators example in java

💻 Program Code

 /*
 * Here we will see the effect of precedence in operators life
 */
class OperatorPrecedenceExample {
 
 public static void main(String args[]) {
 int i = 40;
 int j = 80;
 int k = 40;
 
 int l = i + j / k;
 /*
 * In above calculation we are not using any bracket. So which operator
 * will be evaluated first is decided by Precedence. As precedence of
 * divison(/) is higher then plus(+) as per above table so divison will
 * be evaluated first and then plus.
 *
 * So the output will be 42.
 */
 
 System.out.println("value of L :" + l);
 
 int m = (i + j) / k;
 /*
 * In above calculation brackets are used so precedence will not come in
 * picture and plus(+) will be evaluated first and then divison()/. So
 * output will be 3
 */
 
 System.out.println("Value of M:" + m);
 }
}

                        

🖥 Program Output

value of L :42
Value of M:3
Press any key to continue . . .
                            
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.