GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • C programming
  • C++ programming
  • Java programming
  • Python programming
  • HTML
  • CSS
  • Codenora IDE (opens in a new tab)
  • About us
  • Pricing
  • Contact us
  • Privacy policy
  • Terms of service
  • Refund and Cancellation

This view

  • Original page
  • New address

GeezerVison is showing www.dremendo.com — Operator Precedence in Java Programming | Dremendo

  • Dremendo
  • C
  • C++
  • Java
  • Python
  • HTML
  • CSS
  • More
    • Home
    • C Tutorial
    • C++ Tutorial
    • Java Tutorial
    • Python Tutorial
    • HTML Tutorial
    • CSS Tutorial
?>
profile image
Announcements
No new announcements.
mentora icon
Java Programming Tutorial
  • Java Basic Concepts
    • Introduction
    • Variable
    • Data Types
    • Naming Conventions
    • Integer Variable
    • Long Variable
    • Short Variable
    • Byte Variable
    • Float and Double Variables
    • Character Variable
    • Boolean Variable
    • Constant Variable
    • Typecasting
  • Operators in Java
    • Operators
    • Assignment Operators
    • Arithmetic Operators
    • Relational Operators
    • Logical Operators
    • Operator Precedence
  • Input and Output in Java
    • Environment Setup
    • Program Basic Structure
    • Output
    • Input
    • Comments
  • Decision Making in Java
    • Decision Making
    • if Statement
    • if else Statement
    • if else if Statement
    • Conditional Operator
    • switch case Statement
  • Mathematical Functions
    • Mathematical Functions
  • Loops in Java
    • Loops
    • for Loop
    • while Loop
    • do while Loop
  • Arrays in Java
    • One Dimensional Array
    • Two Dimensional Array
  • String in Java
    • String
    • StringBuffer
    • StringBuilder
  • Searching in Java
    • Linear Search
    • Binary Search
  • Sorting in Java
    • Bubble Sort
    • Selection Sort
    • Insertion Sort
  • Function in Java
    • Function
    • Recursive Function
    • Function Overloading
  • OOPs in Java
    • Classes and Objects
    • Instance Variables
    • Instance Methods
    • Access Modifiers
    • Constructor
    • Static Variables
    • Static Methods
    • Inheritance
    • Interface
    • Types of Inheritance
    • Polymorphism
    • Final Class and Method
    • Abstract Class and Method
    • Wrapper Classes
  • Data Structures in Java
    • Stack
    • Queue
    • Circular Queue
    • Double Ended Queue
    • Singly Linked List
    • Doubly Linked List
    • Stack using Linked List
    • Queue using Linked List
  • Packages in Java
    • Packages
  • Exception Handling in Java
    • Exception Handling
    • User-Defined Exception
  • Thread in Java
    • Thread
    • Thread Methods
  • File Handling in Java
    • Text File Handling
    • Binary File Handling
    • Random Access File Handling

bookmark
Table of Contents

    Operator Precedence in Java Programming

    In this lesson, we will learn what is the rule of Operator Precedence in Java programming and how it works with some examples.

    What is Operator Precedence

    Operator Precedence in Java programming is a rule that describe which operator is solved first in an expression. For example: * and / have same precedence and their associativity is Left to Right, so the expression 18 / 2 * 5 is treated as (18 / 2) * 5.

    Video Explanation

    Click the button below to watch the explanation.

    Please wait while the video is loading....
    video image

    Let's take another example say x = 8 + 4 * 2; here value of x will be 16 and not 24 why, because * operator has a higher precedence than + operator. So 4*2 gets multiplied first and then adds into 8.

    Operator Associativity means how operators of the same precedence are evaluated in an expression. Associativity can be either from left to right or right to left.

    Basic Operator Precedence

    When solving arithmetic equations, follow this order:

    1. Brackets ( )

    If brackets are there in an equation then it will be solved first.

    Example:
      (2 + 3) * 4 -> first do 2 + 3 -> then multiply.

    2. Multiplication, Division, and Modulus: *, /, %

    These are equal in priority. Solve from left to right as they appear in an equation.

    Example:
       12 / 2 * 3
       -> First do 12 / 2 = 6
       -> Then 6 * 3 = 18

    Do not do multiplication before division or vice versa — just go left to right.

    3. Addition and Subtraction: +, −

    These also have equal priority. Solve from left to right as they appear in an equation.

    Example:
       10 − 3 + 2
       -> First do 10 − 3 = 7
       -> Then 7 + 2 = 9

    Now let's see some examples for more understanding.

    Example
    int x = 45 % 2 + 3 * 2;   // result of x is 7
    Explanation
    45 % 2 + 3 * 2
    45 % 2 + 3 * 2   will be solved as per operator precedence rule
    1 + 3 * 2    will be solved as per operator precedence rule
    1 + 6    will be solved as per operator precedence rule
    7 (Answer)
    Example
    int x = 19 + (5 / 2) + 4 % 2 - 6 * 3;   // result of x is 3
    Explanation
    19 + (5 / 2) + 4 % 2 - 6 * 3
    19 + (5 / 2) + 4 % 2 - 6 * 3     will be solved as per operator precedence rule
    19 + 2 + 4 % 2 - 6 * 3     will be solved as per operator precedence rule
    19 + 2 + 0 - 6 * 3     will be solved as per operator precedence rule
    19 + 2 + 0 - 18     will be solved as per operator precedence rule
    21 + 0 - 18     will be solved as per operator precedence rule
    21 - 18     will be solved as per operator precedence rule
    3 (Answer)

    List of Operator Precedence in Java

    The table below shows all the operators in Java with precedence and associativity.

    OPERATOR DESCRIPTION ASSOCIATIVITY
    ( )
    [ ]
    .
    Parentheses
    Array Subscript
    Member Selector
    left to right
    ++
    --
    Post-Increment
    Post-Decrement
    right to left
    ++
    --
    +
    -
    !
    ~
    (type)
    new
    Pre-Increment
    Pre-Decrement
    Unary Plus
    Unary Minus
    Unary Logical Negation
    Unary Bitwise Complement
    Unary Cast
    Object Creation
    right to left
    * / % Multiplication, Division and Modulus left to right
    + - Addition and Subtraction left to right
    <<
    >>
    Bitwise left shift
    Bitwise right shift
    Left to Right
    < <=
    > >=
    relational less than / less than or equal to
    relational greater than / greater than or equal to
    left to right
    == != Relational equal to / not equal to left to right
    & Bitwise AND left to right
    ^ Bitwise exclusive OR left to right
    | Bitwise inclusive OR left to right
    && Logical AND left to right
    || Logical OR left to right
    ? : Ternary operator right to left
    =
    += -=
    *= /=
    %= &=
    ^= |=
    <<= >>=
    >>>
    Assignment operator
    Addition / subtraction assignment
    Multiplication / division assignment
    Modulus and bitwise assignment
    Bitwise exclusive / inclusive OR assignment
    Bitwise left shift / right shift assignment
    Bitwise right shift with zero extension
    right to left

    Test Your Knowledge

    Attempt the questions in the exercise to check whether you have understood the lesson clearly.

    Exercise
    Test Your Knowledge
    Previous Next

    We care about your privacy

    Dremendo uses cookies and similar technologies that are necessary for the site to work properly, understand how you interact with our website, and measure the effectiveness of our marketing. By clicking Accept All, you consent to the use of non-essential cookies and similar technologies for analytics, advertising, and ad measurement, as described in our Privacy Policy.

    Cookie Preferences

    Choose which types of cookies Dremendo may use.

    Necessary Cookies

    Required for essential website functionality and security.

    Allow
    Analytics Cookies

    Help us understand how visitors use Dremendo.

    Allow
    Marketing Cookies

    Help us measure advertising performance and understand interactions with our advertisements.

    Allow
    Mentora Question Asked: 0 / 20

    Hi, I’m Mentora!

    I’m your personal AI tutor from Dremendo. Ask me anything, and I’ll help you understand it in a simple and easy way.

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.