DEV Community

Hanna
Hanna

Posted on

๐Ÿ“ 07. Java Operators: More Than Just Math (and Why Java Feels "Human")

โ— 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. While x++; and ++x; act the same when used alone, they behave completely differently when used with an assignment operator, like z = 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 double has more precision than a float. Therefore, a 0.1 stored as a double is slightly different from a 0.1 stored 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 int to a double for these values returns true. 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 c2 to a char, adding the int 1 turns the whole result back into an int.
  • char c3 = (char)(c2 + 1); -> Correct!
    • You have to wrap the whole expression in parentheses to cast the final result back to a char.

โŒ 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, since float / int results in a float, it fits perfectly into the float variable var2. Good to know!

โŒ Other Weak Spots

  • I keep making mistakes with the ! (NOT) operatorโ€”need more practice.
  • Nested if statements 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)