Relational Operators & boolean Values
Part of: Boolean Expressions & if Statements
What Is a Boolean Expression? A boolean expression is any expression that evaluates to one of two values: true or false. In Java these values have the primitive type boolean . Boolean expressions are the engine behind every decision a program makes. Relational Operators A relational operator compares two values and produces a boolean. Java provides six: - < less than - greater than - <= less than or equal to - = greater than or equal to - == equal to - != not equal to These work on numeric types like int and double. For example: Common Pitfall: == vs = A frequent beginner mistake is confusing assignment (=) with the equality test (==). The single = stores a value into a variable; the double == asks a question and returns a boolean. Writing if (a = b) is a compile error in Java when a and b are int, which actually protects you here. Storing the Result Because the result is a boolean, you can store it in a variable and reuse it: When you print a boolean, Java outputs the literal text true or false. Comparing Doubles With floating-point numbers, exact == comparisons can be unreliable due to rounding, but on the AP exam comparisons like 3.0 == 3.0 behave as expected. For now, just know
Challenge: Compare Two Integers