Premium Only Content
![#9 Assignment operators in JAVA | Skyhighes | Lecture 9](https://1a-1791.com/video/s8/1/h/0/b/f/h0bfp.qR4e-small-9-Assignment-operators-in-J.jpg)
#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!
-
6:01:44
bltgamers
6 hours ago🟢LIVE -Tomb Raider Adventure on Valentine's Day
8472 -
UPCOMING
John Crump Live
20 hours agoLive Call In Show!!!
34 -
1:38:12
The Officer Tatum
2 hours agoLIVE Security Guard POWER SLAPS Black Woman as LEFTISTS Cry OVER RFK Jr. + More Ep 62
20.3K21 -
1:46:13
The Quartering
5 hours agoDOJ Employees QUIT, Mass Federal Layoffs Start, and Kanye Divorce Rumors
53.4K27 -
1:01:13
Dr. Eric Berg
4 days agoThe Dr. Berg Show LIVE February 14, 2025
19.5K3 -
40:17
SLS - Street League Skateboarding
2 days agoEVERY 9 CLUB IN FLORIDA! Looking back at SLS Jacksonville 2021 & 2022 - Yuto, Jagger, Sora & more...
11.5K -
48:05
Stephen Gardner
2 hours ago🔥IT'S OVER! Congress CAN'T HIDE this from Elon MUSK and Trump!!
13.8K15 -
1:01:47
Tucker Carlson
3 hours agoHungary Prime Minister Viktor Orban on USAID, Trump, Immigration, NATO, and the Russia/Ukraine War
104K51 -
1:32:52
Russell Brand
22 hours agoThe Battle for Truth: Gregg Hurwitz on Myth, Power & Cultural Control
105K5 -
1:56:59
The Charlie Kirk Show
4 hours agoVance Buries the Globalists + Inside DOGE + AMA | Dowd | 2.14.2025
143K18