🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Java Notes
Topic #28

Precedence


Java Operator Precedence

When a calculation contains more than one operator, Java follows order of operations rules to decide which part to calculate first.

For example, multiplication happens before addition:

Example

int result1 = 2 + 3 * 4;     // 2 + 12 = 14
int result2 = (2 + 3) * 4;   // 5 * 4 = 20

System.out.println(result1);
System.out.println(result2);

Why Does This Happen?

In 2 + 3 * 4, the multiplication is done first, so the answer is 14.

If you want the addition to happen first, you must use parentheses: (2 + 3) * 4, which gives 20.

Tip: Always use parentheses ( ) if you want to make sure the calculation is done in the order you expect. It also makes your code easier to read.


Order of Operations

Here are some common operators, from highest to lowest priority:

  • () - Parentheses
  • *, /, % - Multiplication, Division, Modulus
  • +, - - Addition, Subtraction
  • >, <, >=, <= - Comparison
  • ==, != - Equality
  • && - Logical AND
  • || - Logical OR
  • = - Assignment

Another Example

Subtraction and addition are done from left to right, unless you add parentheses:

Example

int result1 = 10 - 2 + 5;    // (10 - 2) + 5 = 13
int result2 = 10 - (2 + 5);  // 10 - 7 = 3

System.out.println(result1);
System.out.println(result2);

Note: Remember: Parentheses always come first. Use them to control the order of your calculations.

Want to go beyond the notes?

Join CodingNow 2.0's Java course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Precedence – FAQs

Quick answers about learning Precedence in Java.

This free note from CodingNow 2.0 explains Precedence in Java — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Java topic on CodingNow 2.0, including Precedence, is 100% free with no signup required.
With focused practice, most students grasp Precedence in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now