Code Monkey home page Code Monkey logo

java-compound-assignment-operators's Introduction

Compound Assignment Operators

Learning Goals

  • Learn about compound assignment operators.
  • Show how they work within memory using the browser-based Java Visualizer.

What is a Compound Assignment Operator?

A compound assignment operator combines both an arithmetic operator, like the + operator and the - operator, with the assignment operator =. It is a shortcut way of writing certain expressions. Consider the following table of compound assignment operators:

Operator Description
++ Increment
-- Decrement
+= Addition Compound Assignment Operator
-= Subtraction Compound Assignment Operator
*= Multiplication Compound Assignment Operator
/= Division Compound Assignment Operator
%= Modulus Compound Assignment Operator

These operators can be applied to numerical data types, like int and double. Let's look at the increment and decrement operators first. But to help us, consider the following variables when reading about each operator:

int firstNumber = 20;
int secondNumber = 10;
  • ++ is the increment operator. The increment operator is a unary operator and increases the value of the single operand by 1:
    • firstNumber++ returns 21.
    • Notice that the increment operator is placed directly after the variable name with no space between the variable name and the operand.
  • -- is the decrement operator. The decrement operator is a unary operator and decreases the value of the single operand by 1:
    • secondNumber-- returns 9
    • Notice that the decrement operator is placed directly after the variable name with no spaces between the variable name and the operand.

Note that both the increment and decrement operators operate on the operand and assign it the new value. For example:

firstNumber++; 

Has the exact same result as:

firstNumber = firstNumber + 1; 

Another thing we should note about the increment and decrement operators is that the operator could be placed directly before or after the variable. In the examples above, we placed the operators after the variable. But we could have also done this:

++firstNumber;
--secondNumber;

When we place the operator before the variable, it will still have the same result as before: ++firstNumber will return 21 and --secondNumber will return 9. But order still matters! Let's take a look at an example of when the placement of the increment or decrement operator would make a difference:

int result = ++firstNumber;
System.out.println(result);

// Reset firstNumber to 20
firstNumber = 20;
result = firstNumber++;
System.out.println(result);

Now let's look at the output of the code above:

21
20

So why are the outputs different? When the first line is run with the ++ preceding the variable, Java will first perform the operation of firstNumber = firstNumber + 1; before assigning the value of firstNumber to result. The next time when the increment operator is used is in the format of firstNumber++. When the ++ follows the variable, Java will access the value of firstNumber before completing the increment. This is because:

result = firstNumber++;

is equivalent to

result = firstNumber;
firstNumber = firstNumber + 1;

Therefore, result will be assigned the value 20 before firstNumber is incremented.

To avoid these types of issues, it is best to not combine the assignment operator, =, with the increment or decrement operators in the same statement.

Now let's discuss the other compound assignment operators:

Resources

java-compound-assignment-operators's People

Contributors

kcleland0818 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.