Java: Assignment Operators

Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

You'll find yourself assigning data to variables in Java often. How do you do that? This lesson will define the Java assignment operators and provide code examples of the operators in action.

Assignment Operators

Assignment Operators in Java include mathematical operatros such as plus (+), minus (-), and so on. They are used to give values to variables. There are also shortcut operators which allow programmers to perform functions with less code. The standard operators are:

Operator Function
+ Addition
- Subtraction
* Multiplication
/ Division
% Remainder


There are shortcuts available: these replace longer statements such as x = x + 10. In applications with thousands of lines of code, such shortcuts are an incredibly useful benefit to using assignment operators in Java.

Shortcut Operator Function Use Replaces
++ Plus-Plus n++ n = n + 1
-- Minus-Minus n-- n = n - 1
+= Plus-Equals n+= 5 n = n + 5
-= Minus-Equals n-= 5 n = n - 5
*= Times-Equals n*= 5 n = n * 5
/= Divide-Equals n/=5 n = n / 5
%= Remainder-Equals n%= n = n % 5

General Assignment: Addition, Subtraction, Division, Multiplication

Let's cover the syntax of using general assignment operators.

Addition and Subtraction

The following samples of code show adding a value to a variable, and then subtracting that value from another.

The following code samples show division and multiplication assignment in action:

Java Assignment Addition

The value of j would change from 20 to 15, as it being assigned the value of the sum of k and l.

Now, let's subtract l from k, giving us 5:

Java Assignment Subtraction

Multiplication and Division

The following code shows multiplication in Java:

Java Assignment Multiplication

In the previous code, k will be 45 (15 times 3).

And the converse operation, we can use the division operator to perform simple division.

Java Assignment Division

When the previous code finishes the value of k will be 5.


Very often we have issues with division, in which there is a remainder. The remainder assignment operator (%) will give you the remainder of a division operation.

For example, the following code will return a 0 because 24 divided by 3 is an even number (no remainder):

Java Assignment Remainder

If, however, you tried 25 % 3, the value would be a 1 as there is a remainder. This is great when you want to show if a number has a remainder or not, or in other words, 'Is this number a whole number'?

Shortcut Operators

These operators are used to quickly add one or subtract one from a number. They are used very often in looping (they are a core part of the for loop). Let's look at the most common of these, the increment and decrement operators (++ and --).


Probably the most commonly used assignment operators are ++ and --. They are the core part of for loops and iterative processing. The ++ and -- operators are used to quickly and painlessly increment/decrement a number by 1. This is great when you need to slowly step through operations item by item.

The following example shows a code sample and output value for a loop that will increment 5 times. Pay attention to how the variable is incremented using the ++:

Java Assignment Increment

Here's the output:

Java Assignment Increment

To subtract 1 from a variable each time, we can use --. In the following example, we will start at 100 and reduce by 1 until we hit 0:

Java Assignment Decrement Loop

The value of i will start at 100 and go to 1.

To unlock this lesson you must be a Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use

Become a member and start learning now.
Become a Member  Back
What teachers are saying about
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account