Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 1

Java Operators in Expressions

Recommended resources

Hello! Welcome to the first lesson in our course. We're kicking off the "Java Language Foundations" module, which is the essential first step on your journey to becoming a proficient Java developer.

Today, we will focus on the fundamental building blocks of any Java expression: operators. You'll learn to use arithmetic, relational, logical, and assignment operators to perform calculations, make comparisons, and construct logical conditions. Since you already know Python, many of these concepts will be familiar, but we'll pay close attention to Java's specific syntax and a few important nuances. Mastering operators is crucial, as they form the basis for writing any meaningful code, especially for the conditional logic we'll cover in the next lesson.

Let's begin with a quick visual overview of the different categories of operators in Java.

This chart summarizes the main categories of operators available in Java. In this lesson, we will cover the core types you'll use most frequently: Arithmetic, Assignment, Relational, and Logical.

1. Arithmetic Operators: The Tools for Calculation

Just like in Python, arithmetic operators in Java are used to perform mathematical calculations. These include addition (+), subtraction (-), multiplication (*), division (/), and modulus (%).

A key point to note, especially coming from Python, is how Java handles division. When you divide two integers, Java performs integer division, which truncates any decimal part. For example, 7 / 2 evaluates to 3, not 3.5. To get a floating-point result, at least one of the numbers must be a floating-point type (e.g., 7.0 / 2).

To see these operators in action, let's watch a short segment from a video by Telusko.

#9 Arithmetic Operators in Java

This video provides a clear, hands-on demonstration of the basic arithmetic operators.

Watch the first part of the video, where the presenter explains and demonstrates addition, subtraction, multiplication, integer division, and the modulus operator. Pay close attention to the 7 / 5 example to see integer division at work. You can watch from the beginning up to the end of the modulus explanation.

Increment and Decrement Operators

Java also provides special unary operators for incrementing (++) and decrementing (--) a variable's value by 1. These are very common in loops and counters. For example, counter++ is equivalent to counter = counter + 1.

A subtle but important distinction exists between pre-increment (++variable) and post-increment (variable++).

  • Pre-increment (++variable): The value is incremented first, and the new value is used in the expression.
  • Post-increment (variable++): The original value is used in the expression first, and then the value is incremented.

This distinction is crucial for avoiding bugs when these operators are used within larger expressions or assignments. The same logic applies to the pre-decrement (--variable) and post-decrement (variable--) operators.

The same video from Telusko has an excellent explanation of this concept.

#9 Arithmetic Operators in Java

This part of the video breaks down the difference between pre- and post-increment, which is a classic source of confusion for new Java programmers.

Watch the section from increment/decrement operators. The presenter does a great job of showing how the behavior changes when you assign the result of the operation to another variable.


2. Assignment Operators: Shortcuts for Updating Variables

Assignment operators are used to assign values to variables. You're already familiar with the basic assignment operator, =, from both Python and your first look at Java variables.

Java also includes compound assignment operators that combine an arithmetic operation with an assignment. These provide a convenient shorthand. For example, score += 10; is a more concise way of writing score = score + 10;.

The Programiz article on Java operators provides a very clear table and examples for these.

Java Operators: Arithmetic, Relational, Logical and more

This section from Programiz offers a quick and effective summary of all the assignment operators.

Please read the section titled Java Assignment Operators. Focus on the table that shows each operator and its long-form equivalent. Then, review the code example to see how they are used.


3. Relational and Logical Operators: The Basis of Decision-Making

These two groups of operators are the foundation of control flow. They allow you to create complex conditions that evaluate to either true or false.

Relational operators compare two values. They include:

  • == (Equal to)
  • != (Not equal to)
  • > (Greater than)
  • < (Less than)
  • >= (Greater than or equal to)
  • <= (Less than or equal to)

Logical operators combine boolean expressions. The main ones are:

  • && (Logical AND): true if both operands are true.
  • || (Logical OR): true if at least one operand is true.
  • ! (Logical NOT): Inverts the boolean value (e.g., !true is false).

These are syntactically different from Python's and, or, and not, but their function is identical. A video from Alex Lee provides a very practical guide to using these operators.

Logical Operators In Java #20

This video clearly explains each logical operator and then shows how to combine them with relational operators to build useful conditions.

Watch the following segments: The introduction to the three logical operators from the beginning. The explanations of the OR, AND, and NOT operators from the examples. Finally, watch how these are used with numbers and comparative operators, which is a very common use case, from this section. The final part of the video previews if statements, which we will cover in our next lesson.


4. Operator Precedence: The Rules of the Road

What happens when you have an expression like boolean isValid = x > 5 && y < 10;? How does Java know which operation to perform first?

The answer lies in operator precedence, which is a set of rules that defines the order in which operators are evaluated. This is similar to the PEMDAS/BODMAS rule you learned in math. For example, multiplication (*) has a higher precedence than addition (+), so 5 + 3 * 2 evaluates to 11, not 16.

Understanding precedence is key to writing correct and predictable code.

Operators in Java (Examples and Practice)

This article from CodeChef provides a simplified and clear explanation of operator precedence.

Read the section titled Operator Precedence. Pay close attention to the numbered list which shows the hierarchy from highest to lowest precedence. Notice that unary operators like ++ have very high precedence, while assignment operators have the lowest.

As a best practice, even if you know the precedence rules, it's often better to use parentheses () to make your code more readable and to explicitly enforce the order of operations. For example, (5 + 3) * 2 leaves no doubt about your intention. This is a hallmark of professional, maintainable code.


Key Takeaways

  • Arithmetic Operators (+, -, *, /, %) perform calculations. Remember that integer division in Java truncates the result.
  • Increment/Decrement (++, --) are shortcuts for adding or subtracting 1. The distinction between pre- and post-operation is critical.
  • Assignment Operators (=, +=, -=) assign or update the value of variables.
  • Relational (==, !=, >) and Logical (&&, ||, !) Operators are used to create boolean expressions that evaluate to true or false.
  • Operator Precedence determines the order of evaluation in complex expressions. Use parentheses to ensure clarity and correctness.

In our next lesson, we will put these operators to immediate use by exploring conditional statements like if-else and switch, allowing our programs to make decisions and execute different code paths based on conditions.