> For the complete documentation index, see [llms.txt](https://www.pranaypourkar.co.in/the-programmers-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.pranaypourkar.co.in/the-programmers-guide/java/java-basics/java-operators/operator-precedence.md).

# Operator Precedence

In Java, operator precedence determines the order in which operators are evaluated in an expression. Operators with higher precedence are evaluated before operators with lower precedence. If operators have the same precedence, they are evaluated from left to right.

Here's a general overview of operator precedence in Java, from highest to lowest precedence:

1. Postfix operators: `expr++`, `expr--`
2. Unary operators: `++expr`, `--expr`, `+expr`, `-expr`, `!expr`, `~expr`, `(type)expr`
3. Multiplicative operators: `*`, `/`, `%`
4. Additive operators: `+`, `-`
5. Shift operators: `<<`, `>>`, `>>>`
6. Relational operators: `<`, `<=`, `>`, `>=`, `instanceof`
7. Equality operators: `==`, `!=`
8. Bitwise AND: `&`
9. Bitwise XOR: `^`
10. Bitwise OR: `|`
11. Logical AND: `&&`
12. Logical OR: `||`
13. Ternary operator: `? :`
14. Assignment operators: `=`, `+=`, `-=`, `*=`, `/=`, `%=`, `&=`, `^=`, `|=`, `<<=`, `>>=`, `>>>=`

**Parentheses `()` can be used to override the default precedence and force evaluation of parts of an expression first.**

For example:

```java
int result = 10 + 2 * 3;
```

In this expression, multiplication has higher precedence than addition, so `2 * 3` is evaluated first, resulting in `6`, and then added to `10`, yielding `16`.

Understanding operator precedence is crucial for writing expressions that produce the expected results and avoiding bugs related to incorrect evaluation order.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://www.pranaypourkar.co.in/the-programmers-guide/java/java-basics/java-operators/operator-precedence.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
