πŸ“š

All Subjects

Β >Β 

πŸ’»Β 

AP Comp Sci A

Β >Β 

βž•

Unit 1

1.4 Compound Assignment Operators

2 min readβ€’november 16, 2020

peter-cao

Peter Cao


AP Computer Science AΒ πŸ’»

BookmarkedΒ 2.4kΒ β€’Β 93Β resources
See Units

1.4: Compound Assignment Operators

Compound Operators

Sometimes, you will encounter situations where you need to perform the following operation:
int integerOne = 6; integerOne = integerOne * 2;
This is a bit clunky with the repetition of integerOne in line two. We can condense this with this statement:
integerOne *= 2;
The "*= 2" is an example of a compound assignment operator, which multiplies the current value of integerOne by 2 and sets that as the new value of integerOne. Other arithmetic operators also have compound assignment operators as well, with addition, subtraction, division, and modulo having +=, -=, /=, and %=, respectively.

Incrementing and Decrementing

There are special operators for the two following operations in the following snippet well:
integerOne += 1; integerTwo -= 1;
These can be replaced with a pre-increment/pre-decrement (++i or - -i) or post-increment/post-decrement (i++ or i- -) operator. You only need to know the post-variant in this course, but it is useful to know the difference between the two. Here is an example demonstrating the difference between them:
int integerOne = 2; integerOne++; System.out.println(integerOne); ++integerOne; System.out.println(integerOne); System.out.println(integerOne++); System.out.println(++integerOne); 3 4 4 6
By itself, there is no difference between the pre-increment and post-increment operators, but it's evident when you use it in a method such as the println method. For this statement, I will write a debugging output, which happens when we trace the code, which means to follow it line-by-line.
Value of integerOne after line 1: 2 Value of integerOne after line 2: 3 Value of integerOne after line 3: 3 Value of integerOne after line 4: 4 Value of integerOne after line 5: 4 Value of integerOne before printing on line 6: 4 Value of integerOne after line 6: 5 (post-increment increments after the method) Value of integerOne before printing on line 7: 6 Value of integerOne after line 7: 6 (pre-increment increments before the method)

Was this guide helpful?

Join us on Discord
Thousands of students are studying with us for the AP Computer Science A exam.
join now
Hours Logo
Studying with Hours = the ultimate focus mode
Start a free study session
πŸ” Are you ready for college apps?
Take this quiz and find out!
Start Quiz
Browse Study Guides By Unit
πŸ™Exam Reviews
πŸ–±Unit 10: Recursion
βž•Unit 1: Primitive Types
πŸ“±Unit 2: Using Objects
πŸ–₯Unit 3: Boolean Expressions and if Statements
πŸ•ΉUnit 4: Iteration
βš™οΈUnit 5: Writing Classes
⌚️Unit 6: Array
πŸ’ΎUnit 7: ArrayList
πŸ’»Unit 8: 2D Array
πŸ–²Unit 9: Inheritance
FREE AP comp sci a Survival Pack + Cram Chart PDF
Sign up now for instant access to 2 amazing downloads to help you get a 5
Join us on Discord
Thousands of students are studying with us for the AP Computer Science A exam.
join now
πŸ’ͺ🏽 Are you ready for the Comp Sci exam?
Take this quiz for a progress check on what you’ve learned this year and get a personalized study plan to grab that 5!
START QUIZ
πŸ“± Stressed or struggling and need to talk to someone?
Talk to a trained counselor for free. It's 100% anonymous.
Text FIVEABLE to 741741 to get started.
Β© 2021 Fiveable, Inc.