About The Author
Hi, my name is Casey, I am sharing my journey and tips while learning to code.

Operator Precedence in Java | Day 3 of 100daysofcode

by Casey Spaulding

Today was a deep dive into operator precedence in Java. It mostly follows the same rules as PEMDAS P- Parentheses, E- Exponents, M- Multiplication, D- Division, A- Addition, and S- Subtraction for order of operations.

Seems like a simple concept but it did get tricky for me once you start adding the modulus operator into a long equation such as 15 / 3 % 2 * (6+2) or 9 % 5 % 2 equals what?

So what I learned was that * takes precedence over the % operator and if you have several modulus operators next to each other it is left to right. Here is a good chart from Princeton.edu

This image is from StackOverflow https://stackoverflow.com/questions/66519436/java-order-of-operations-clarification

You may also like

Leave a Comment