← Back to Chapters

Java Assignment Operators

? Java Assignment Operators

? Quick Overview

Assignment operators in Java are used to assign values to variables. They can also combine assignment with arithmetic or logical operations to make code shorter and more readable.

? Key Concepts

  • = assigns a value
  • +=, -=, *=, /= combine operation with assignment
  • %= assigns remainder
  • Assignment operators work from right to left

? Syntax & Theory

The basic assignment operator is =. Java also provides compound assignment operators that perform an operation and assignment in one step.

? Code Examples

? View Code Example
// Demonstrating basic assignment operators in Java
int a = 10;
int b = 5;

a += b;
a -= b;
a *= b;
a /= b;
a %= b;

System.out.println(a);

? Live Output / Explanation

Each compound assignment operator updates the value of variable a by performing the operation using b and storing the result back into a.

✅ Tips & Best Practices

  • Use compound operators to write cleaner and shorter code
  • Be careful with division assignment as it may truncate values
  • Always understand operator precedence

? Try It Yourself

  • Create two variables and apply all assignment operators
  • Predict the output before running the program
  • Test with different data types like double