What values can a Java boolean store?
A boolean stores exactly one of two values: true or false. Java conditions must have type boolean or Boolean; integers such as 1 and 0 cannot serve as conditions automatically.
Study Java Boolean Logic and Selection with 12 free online flashcards. Review key terms, definitions, and concepts with this interactive flashcard deck.
What values can a Java boolean store?
A boolean stores exactly one of two values: true or false. Java conditions must have type boolean or Boolean; integers such as 1 and 0 cannot serve as conditions automatically.
How do = and == differ in Java?
= assigns a value to a variable, while == tests whether two values are equal. For example, score = 90 changes score; score == 90 produces a boolean result.
How should Java String contents be compared?
For primitive numeric values, == compares the actual values. For String objects, == compares references; use equals to compare character contents, such as first.equals(second).
What do !, &&, and || mean?
! reverses a boolean, && is true only when both operands are true, and || is true when at least one operand is true.
What is Java’s logical operator precedence?
Without parentheses, Java evaluates logical operators in this order: ! first, then &&, then ||. Parentheses make compound conditions clearer and can change the grouping.
How does short-circuit evaluation work?
With &&, a false first operand prevents evaluation of the second. With ||, a true first operand prevents evaluation of the second. This can avoid errors such as division by zero.
What does a one-way if statement do?
An if statement executes its body only when its boolean condition is true. If the condition is false, the body is skipped and execution continues afterward.
When should you use if-else?
An if-else statement chooses exactly one of two bodies: the if body when the condition is true, or the else body when it is false.
How does an if-else-if chain choose a branch?
An if-else-if chain checks conditions from top to bottom and executes only the first branch whose condition is true. Therefore, condition order matters.
What is a nested conditional?
A nested conditional is a conditional inside another conditional. The inner condition is evaluated only if execution reaches it, such as after the outer condition is true.
Which if does an else match?
An else attaches to the nearest unmatched if at the same structural level. Braces make the intended association explicit and prevent ambiguity.
What are De Morgan’s laws?
De Morgan’s laws are !(a && b) equivalent to !a || !b, and !(a || b) equivalent to !a && !b. Negation switches AND and OR while negating each condition.