โ Study period: 260313
โ Study scope: Honja 3-01,02 part operator part
Today, I dived into Java operators. I encountered some unfamiliar types and learned about their real-world applications. Hereโs a summary of what I covered!
1. Essential Operators You Need to Know
-
% (Remainder Operator): It gives you the "leftover" value after division.
- I used to think this was useless, but itโs actually super handy for distinguishing between even/odd numbers or checking for multiples in practice.
-
Logical Operators (&&, ||, ^, !)
-
&&,&(AND): Returns true only if both conditions are met. -
||,|(OR): Returns true if at least one condition is met. -
^(XOR): Returns true only if the two values are different. -
!,!=(NOT): Reverses the result (Negation). - The "!" (Exclamation Mark) is crucial: It's used constantlyโfor example, "If NOT logged in (!isLoggedIn), block the page." It's a bit of a brain teaser, but I need to master this now so I don't get confused later in the field!
-
2. Increment & Decrement Operators (++, --)
These are tricky because their value changes depending on whether you put them before or after the variable.
- ++x (Prefix): The value is incremented first, then used in the expression.
- x++ (Postfix): The current value is used first, then incremented later.
๐ก Real-world Example:
I understood this better when I thought about an AI picking up game items and filling slots sequentially, or managing queue numbers. Whilex++;and++x;act the same when used alone, they behave completely differently when used with an assignment operator, likez = x++;.
3. The Betrayal of Floating Points: Why 0.1 == 0.1f is False
This was the most surprising part of today's study.
- Computers speak Binary: Decimal numbers that don't terminate in binary are inherently imprecise.
-
double vs float: A
doublehas more precision than afloat. Therefore, a0.1stored as a double is slightly different from a0.1stored as a float. Even if you move a float value into a double "container," the precision doesn't magically increase!
๐ค Computers Are Kind of Human?
Interestingly, computers love floating-point numbers that terminate perfectly in binary.
- Examples: 1.0, 0.5, 0.25, 0.125 (powers of 2).
- These have zero error, so comparing an
intto adoublefor these values returnstrue. It reminded me of my part-time job at the convenience storeโI love it when customers give me the exact change so I don't have to deal with coins. Computers are the same! lol.
4. Review Points (My "Oops" Note)
โ char Operation Errors
-
char c3 = (char)c2 + 1;-> Error!-
Why? Even if you cast
c2to achar, adding theint1 turns the whole result back into anint.
-
Why? Even if you cast
-
char c3 = (char)(c2 + 1);-> Correct!- You have to wrap the whole expression in parentheses to cast the final result back to a
char.
- You have to wrap the whole expression in parentheses to cast the final result back to a
โ The float Division Surprise
-
float var2 = var1 / 100;(where var1 is a float)- I thought this might cause an error since Javaโs default for decimals is
double. However, sincefloat / intresults in afloat, it fits perfectly into the float variablevar2. Good to know!
- I thought this might cause an error since Javaโs default for decimals is
โ Other Weak Spots
- I keep making mistakes with the
!(NOT) operatorโneed more practice. - Nested
ifstatements with multiple parentheses still make my head spin. - I need to review
Double.parseDouble()for converting strings to data.
๐ Wrapping Up
The problems are getting spicier as I move through the operators. My head hurts a bit. ๐ญ Can I really finish this whole book on time? Can I even make it through the first read-through? Let's stay focused. Fighting! ๐ฅ
Top comments (0)