Premium Only Content
#9 Assignment operators in JAVA | Skyhighes | Lecture 9
Java Assignment Operators: The Building Blocks of Code
Assignment operators in Java are the workhorses of your code, letting you assign values to variables. Think of them as tiny shovels, diligently scooping up values and placing them into their designated variable buckets. Mastering these operators is crucial for building anything from simple calculations to complex applications.
Meet the Operators:
Here's a rundown of the most common assignment operators and their actions:
Simple Assignment Operator (=):
This classic operator is the bread and butter of assignments. It assigns the value on the right to the variable on the left.
Java
int age = 25; // Assigns 25 to the variable "age"
String name = "John"; // Assigns "John" to the variable "name"
Use code with caution. Learn more
Compound Assignment Operators:
These operators combine assignment with an operation, making your code concise and efficient.
Addition Assignment Operator (+=): Adds the right operand to the left variable and then assigns the new value.
Java
int score = 10;
score += 5; // Increases score by 5, resulting in 15
Use code with caution. Learn more
Subtraction Assignment Operator (-=): Subtracts the right operand from the left variable and then assigns the new value.
Java
int lives = 3;
lives -= 1; // Decreases lives by 1, resulting in 2
Use code with caution. Learn more
Multiplication Assignment Operator (*=): Multiplies the right operand with the left variable and then assigns the new value.
Java
float price = 5.0f;
price *= 1.1f; // Increases price by 10%, resulting in 5.5f
Use code with caution. Learn more
Division Assignment Operator (/=): Divides the left variable by the right operand and then assigns the new value.
Java
int distance = 100;
distance /= 2; // Divides distance by 2, resulting in 50
Use code with caution. Learn more
Modulo Assignment Operator (%=): Performs the modulo operation (remainder after division) on the left and right operands and then assigns the new value.
Java
int ageInYears = 27;
ageInYears %= 12; // Calculates remainder when dividing by 12 (useful for finding months from years)
Use code with caution. Learn more
Shift Assignment Operators:
These operators shift the bits of the left operand by a specified amount and then assign the new value. They're mainly used for low-level bit manipulation.
Left Shift Assignment Operator (<<=): Shifts the bits of the left operand to the left by the specified amount.
Right Shift Assignment Operator (>>=): Shifts the bits of the left operand to the right by the specified amount.
Right Shift Assignment with Sign Extension (>>>=): Similar to >>=, but fills empty bits with the sign bit of the operand to preserve signed values.
Remember:
Assignment operators only work with variables, not expressions directly.
Operators like += and -= work only with compatible data types (e.g., integers with integers).
Use assignment operators judiciously to avoid accidental reassignment or data loss.
Bonus Tip: Check out the Java operator precedence table to understand the order in which different operators are evaluated in an expression.
With this understanding of assignment operators, you're well on your way to building strong foundations in Java. So grab your metaphorical shovel and start crafting some amazing code!
-
2:34:52
Danny Polishchuk
19 hours agoTrudeau Out, 2025 Starts With a Bang + The Las Vegas Shooting Conspiracy
83.6K28 -
51:58
Adam Does Movies
18 hours ago $4.37 earnedReacting To The Golden Globe Awards Winners! - LIVE
41.1K1 -
1:06:12
Flyover Conservatives
1 day agoJ6: A True Timeline Documentary - In Honor of the 4 Year Anniversary of January 6 | FOC Show
59.3K18 -
1:08:03
Donald Trump Jr.
18 hours agoJanuary 6th: More to Uncover, Live with Rep Loudermilk & Darren Beattie | TRIGGERED Ep.205
260K305 -
1:00:48
The StoneZONE with Roger Stone
11 hours agoJustin Trudeau Throws In The Towel! w/ Canadian Hockey Legend Theo Fluery | The StoneZONE
96.6K26 -
1:18:37
We Like Shooting
21 hours ago $3.47 earnedDouble Tap 391(Gun Podcast)
59K1 -
2:32:43
FreshandFit
21 hours agoAndrew Wilson VS Gary The Numbers Guy Astrology & Numerology Debate!
139K86 -
9:22:40
Dr Disrespect
19 hours ago🔴LIVE - DR DISRESPECT - TRIPLE THREAT CHALLENGE - RIVALS, PUBG, WARZONE
235K46 -
1:15:03
Dad Dojo Podcast
18 hours ago $1.70 earnedEP16: Parenting: Then vs Now
47.1K -
53:58
Sarah Westall
12 hours agoBlack Pilled vs Learning Reality and Maintaining Perspective, AI Reality w/ Johnny Vedmore
69.5K4