Sub topic 6
Adding Conditions with `&&`
This chapter assumes you know basic Java syntax, reference types, instanceof, casts, boolean operators, and the idea of flow-scoped pattern variables.
A type pattern can do more than choose an if branch. You can combine it with additional tests using the short-circuit AND operator, &&.
The basic pattern
Suppose a method receives an Object and should return true only for a long string:
public class Main {
static boolean isLongString(Object value) {
return value instanceof String text
&& text.length() > 10;
}
public static void main(String[] args) {
System.out.println(isLongString("hello"));
System.out.println(isLongString("This string is long"));
System.out.println(isLongString(42));
}
}The condition has two parts:
value instanceof String texttests the runtime type and createstext.text.length() > 10checks the value after the type test succeeds.
The second part can use text because && evaluates its right side only when its left side is true. Therefore, when text.length() runs, text is definitely a String.
This behavior is called flow scoping. The compiler tracks the paths through the condition and makes a pattern variable available only where the pattern is known to have matched.
Combining several conditions
You can add more tests with additional && operators:
static boolean isUsefulLabel(Object value) {
return value instanceof String text
&& !text.isBlank()
&& text.length() <= 40;
}Java evaluates these conditions from left to right. It stops at the first false condition. This is called short-circuit evaluation.
The order matters. The type pattern must come first because later conditions use its binding variable:
return value instanceof String text
&& text.length() > 10
&& text.charAt(0) != ' ';The compiler knows that text is available in both later conditions. Each one runs only after all conditions before it have succeeded.
| Condition | What it guarantees |
|---|---|
| `value instanceof String text` | `text` exists and has type `String` |
| `text.length() > 10` | The string has more than ten characters |
| `text.charAt(0) != ' '` | The first character is not a space |
Conditions inside an `if` statement
The same rule applies when the complete condition controls an if statement:
if (value instanceof String text && text.length() > 10) {
System.out.println("Long string: " + text);
}Inside the if body, both tests have succeeded, so text is available. In the else body, it is not available because the condition may have failed before the pattern matched.
A common mistake is to separate the test from the dependent condition with ||:
// Does not compile:
// value instanceof String text || text.length() > 10If the left side is false, Java evaluates the right side. In that path, the pattern did not match, so text cannot be used safely. Use && when the second test requires the matched type.
Exercise
Write a method that returns true only when its argument is a non-empty String with at least five characters.
Show answer
static boolean valid(Object value) { return value instanceof String text && !text.isEmpty() && text.length() >= 5; }