CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Java > PEMDAS in Java: Order of Operations Explained with Examples

Java Tutorial

  • Introduction
    • What is Java
    • History Of Java
    • Install Java
    • What is JVM
    • JDK vs JRE vs JVM
    • Java Bytecode
    • OOP vs POP
    • Compile and Run Java
  • Tokens, Expressions and Control Structures
    • Primitive data types
    • Integers
    • Floating Points
    • Characters
    • Booleans
    • User Defined Data Type
    • Declarations
    • Constants
    • Identifiers
    • Literals
    • Type Conversion and Casting
    • Variables
    • Default Variable Initialization
    • Command Line Arguments
    • Arrays of Primitive Types
    • Comment Syntax
    • Garbage Collection
    • Expressions
    • Operators
    • Arithmetic Operator
    • Bitwise and Shift Operator
    • Comparison or Relational Operators
    • Logical Operators
    • Assignment Operators
    • Ternary Operator
    • Increment and Decrement Operator
    • Control Statements
  • OOP Concepts
    • Class and Object
    • Create Class Instance
    • Method
    • Abstraction
    • Encapsulation
    • this keyword
    • Constructor
    • Pass by Value
    • Access Modifier/Control
    • Polymorphism
    • Method Overloading vs Method Overriding
    • Recursion
    • Nested and Inner Class
  • Inheritance and Packaging
    • Inheritance
    • extends Keyword
    • super Keyword
    • Object Class
    • Abstract class
    • Final Class
    • Java Package
    • Interface
  • Handling Error/ Exceptions
    • What is Exception
    • Exception Handling Keywords
    • Common Java Errors
    • User Defined Exception
    • Throwing and re-throwing Exception
    • finally Block
  • Strings
    • Java String Tutorial
  • Threads
    • Introduction
    • Create Thread
    • Thread Lifecycle
    • Thread Priority
    • Thread Synchronization
    • Inner Thread Communication
    • Thread Deadlock
  • IO and Streams
    • java.io Package
    • Files and Directories
    • Byte Stream
    • Character Stream
    • Console Input and Output
    • Serializable and Deserializable
  • Core Packages
    • java.lang Package
    • Math
    • Wrapper Classes
    • java.lang.Number
    • Double
    • Float
    • Integers
    • java.lang.Byte
    • java.lang.Short
    • java.lang.Long
    • java.lang.Character
    • java.lang.Boolean
    • java.util package
    • Vector Class
    • Stack Class
    • Dictionary Class
    • Hashtable
    • Enumeration or Enum
    • Generate Random Number
  • Holding Collection of Data
    • Arrays
    • Map
    • List
    • Set
    • Collection Interface
    • Collections Class
    • ArrayList
    • HashSet
    • TreeSet
    • Comparator
  • Java Bean
    • What is Java Bean
    • Advantages and Disadvantages of Java Bean
    • Java Beans API
    • Introspection
    • Java Bean Properties
    • Bound and Constrained Properties
    • BeanInfo Interface
    • Customizers
    • Java Beans Persistence
    • BeanDescriptor
  • Home

PEMDAS in Java: Order of Operations Explained with Examples

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Last updated: July 1, 2026 · 2 min read · 0 Comments
Share: in X

PEMDAS is a mnemonic for the order of operations in arithmetic. It ensures calculations are performed consistently. In Java, it governs how expressions like 5 + 3 * 2 are evaluated. Let’s break it down!

Table of Contents

  • PEMDAS Breakdown
    • P – Parentheses ()
    • E – Exponents
    • M/D – Multiplication * / Division /
    • A/S – Addition + / Subtraction -**
  • PEMDAS in Java: Examples
    • Example 1: Basic Arithmetic
    • Example 2: Parentheses Override
    • Example 3: Division vs. Multiplication
    • Example 4: Modulus Operator (%)
  • Common Mistakes to Avoid
    • Ignoring integer division
    • Misplacing operations
    • Forgetting parentheses
  • Java Operator Precedence Table (Simplified)
  • Why PEMDAS Matters in Java

PEMDAS Breakdown

P – Parentheses ()

Highest precedence. Operations inside parentheses are evaluated first.

int result = (10 + 5) * 2; // 15 * 2 = 30  

E – Exponents

Java doesn’t have a built-in exponent operator. Use Math.pow() instead:

double power = Math.pow(2, 3); // 8.0 (2³)  

M/D – Multiplication * / Division /

Equal precedence. Evaluated left to right.

int x = 10 / 2 * 5; // (10/2)=5 → 5*5=25  

A/S – Addition + / Subtraction -**

Equal precedence. Evaluated left to right.

int y = 20 - 5 + 3; // (20-5)=15 → 15+3=18  

PEMDAS in Java: Examples

Example 1: Basic Arithmetic

int result = 5 + 3 * 2; // 3*2=6 → 5+6=11  

Example 2: Parentheses Override

int result = (5 + 3) * 2; // 8*2=16  

Example 3: Division vs. Multiplication

int result = 10 / 2 * 5; // 5*5=25 (not 10/(2*5)=1)  

Example 4: Modulus Operator (%)

Same precedence as multiplication/division:

int value = 10 + 15 % 4; // 15%4=3 → 10+3=13  

Common Mistakes to Avoid

Ignoring integer division

    double avg = (5 + 3) / 2; // Result: 4.0 (not 4.5)  
    // Fix:  
    double avg = (5 + 3) / 2.0; // 4.5  

    Misplacing operations

    int result = 5 + 2 * 3; // 11 (not 21)  

    Forgetting parentheses

    boolean isEven = 10 % 2 == 0; // Correct  
    boolean isEven = (10 % 2) == 0; // Better for clarity  

    Java Operator Precedence Table (Simplified)

    PrecedenceOperators
    Highest()
    *, /, %
    Lowest+, -

    Why PEMDAS Matters in Java

    • Ensures calculations are predictable.
    • Prevents logical errors in math-heavy code (e.g., financial apps).
    • Improves readability when used with parentheses.

    Related Posts:

    • Arithmetic Operator in Java
    • Expressions in Java
    • Control Statements in Java
    • Integers In Java
    • MySQL Commands for Developers
    • Floating point in Java
    Tags:javalanguage-fundamentals
    Was this article helpful?
    ← Previous ArticleSpring Boot Async Tasks: A Complete Guide to Asynchronous Execution
    Next Article →The Four Pillars of Object Oriented Programming (OOP)

    Leave a Comment Cancel reply

    You must be logged in to post a comment.

    Recent Posts

    • How to implement Passwordless Authentication in Spring Boot: A Step-by-Step Guide
    • How to Use AWS CloudFront Signed URLs in Spring Boot?
    • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
    • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
    • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
    CoderSathi

    Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

    Linkedin

    Quick Links

    • About
    • Contact

    Popular Topics

    • Java
    • Spring Boot
    • AWS
    • DevOps
    • MongoDB
    • Linux
    • Git
    • How to
    © 2026 CoderSathi. All rights reserved. Privacy Policy · Sitemap