bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/Java/Java Tutorial
Java•Java Tutorial

Java Assignment Operators

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind Java Assignment Operators?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Order

Put the learning moves in the order that makes the concept easiest to apply.

Assignment operators are used to assign values to variables.
Real-Life Example: Tracking Savings
Assignment Operators

Assignment Operators

Assignment operators are used to assign values to variables.

In the example below, we use the assignment operator ( = ) to assign the value 10 to a variable called x :

int x = 10;

The addition assignment operator ( += ) adds a value to a variable:

int x = 10;
x += 5;

A list of all assignment operators

OperatorExampleSame As
=x = 5x = 5
+=x += 3x = x + 3
-=x -= 3x = x - 3
*=x *= 3x = x * 3
/=x /= 3x = x / 3
%=x %= 3x = x % 3
&=x &= 3x = x & 3
=x= 3x = x3
^=x ^= 3x = x ^ 3
>>=x >>= 3x = x >> 3
<<=x <<= 3x = x << 3

Note

Most assignment operators are just shorter ways of writing code. For example, x += 5 is the same as x = x + 5 , but shorter and often easier to read.

Real-Life Example: Tracking Savings

Assignment operators can also be used in real-life scenarios. For example, you can use the += operator to keep track of savings when you add money to an account:

Example

int savings = 100;
savings += 50; // add 50 to savings
System.out.println("Total savings: " + savings);

Previous

Java Arithmetic Operators

Next

Java Comparison Operators