You have two options.

(1) You can enable the preview feature in Java 14, by compiling with

javac MainClass.java --enable-preview --release 14

and running with

java MainClass --enable-preview

(2) The line you wrote is equivalent to this.

if (this.areas.get(i) instanceof Habitat) {
    Habitat area = (Habitat) this.areas.get(i);

    // ... more here

Assuming, of course, that this get method doesn't have any nasty side-effects. This is how you do it if you don't want to enable the preview feature.

Answer from Dawood ibn Kareem on Stack Overflow
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › language › pattern-matching-instanceof.html
Pattern Matching for instanceof - Java
October 20, 2025 - A pattern is a combination of a predicate, or test, that can be applied to a target and a set of local variables, called pattern variables, that are assigned values extracted from the target only if the test is successful. The predicate is a Boolean-valued function of one argument; in this case, it’s the instanceof operator testing whether the Shape argument is a Rectangle or a Circle.
🌐
Baeldung
baeldung.com › home › java › core java › pattern matching for instanceof in java
Pattern Matching for instanceof in Java | Baeldung
January 16, 2024 - Java 14, via JEP 305, brings an improved version of the instanceof operator that both tests the parameter and assigns it to a binding variable of the proper type.
🌐
GeeksforGeeks
geeksforgeeks.org › java › pattern-matching-for-instanceof-java-17
Pattern Matching For instanceof Java 17 - GeeksforGeeks
July 23, 2025 - Java 14, via JEP 305, brings an enhanced version of the instanceOf keyword that both tests the parameter object and assigns it to a binding variable of the correct type.
🌐
OpenRewrite
docs.openrewrite.org › recipe catalog › static analysis and remediation › changes code to use java 17's `instanceof` pattern matching
Changes code to use Java 17's instanceof pattern matching | OpenRewrite Docs
@@ -14,3 +14,3 @@ class Foo { void bar(Object o1, Object o2) { - if (o1 instanceof LeftNode && o2 instanceof RightNode) { - ((LeftNode)o1).bar(); - ((RightNode)o2).bar(); + if (o1 instanceof LeftNode node2 && o2 instanceof RightNode node3) { + node2.bar(); + node3.bar(); } @@ -18,3 +18,3 @@ ((RightNode)o2).bar(); } - else if (o1 instanceof RightNode && o2 instanceof LeftNode) { - ((RightNode)o1).bar(); - ((LeftNode)o2).bar(); + else if (o1 instanceof RightNode node && o2 instanceof LeftNode node1) { + node.bar(); + node1.bar(); } This recipe has no required configuration options. It can be act
Published   3 days ago
🌐
OpenJDK
openjdk.org › jeps › 394
JEP 394: Pattern Matching for instanceof
July 27, 2020 - Make it a compile-time error for a pattern instanceof expression to compare an expression of type S against a pattern of type T, where S is a subtype of T. (This instanceof expression will always succeed and is then pointless. The opposite case, where a pattern match will always fail, is already a compile-time error.) Other refinements may be incorporated based on further feedback. Nearly every program includes some sort of logic that combines testing if an expression has a certain type or structure, and then conditionally extracting components of its state for further processing. For example, all Java programmers are familiar with the instanceof-and-cast idiom:
🌐
Baeldung
baeldung.com › home › java › core java › java instanceof operator
Java instanceof Operator | Baeldung
May 11, 2024 - Technically speaking, we’re only allowed to use instanceof along with reified types in Java.
🌐
Medium
medium.com › @alxkm › javas-pattern-matching-simplifying-conditional-logic-and-type-checking-f9f1425f7c03
Java’s Pattern Matching: Simplifying Conditional Logic and Type Checking | by Alex Klimenko | Medium
July 11, 2025 - Pattern matching for instanceof is a feature introduced in Java 16 and refined in Java 17 as a preview feature in JEP 406. It simplifies the common pattern of checking an object’s type and then casting it to that type.
Find elsewhere
🌐
Medium
medium.com › tuanhdotnet › pattern-matching-with-instanceof-in-java-17-ebc7e142b8cc
Pattern Matching with instanceof in Java 17 | by Anh Trần Tuấn | tuanhdotnet | Medium
April 24, 2025 - In versions prior to Java 17, you might see code like this: Object obj = "Hello, World!"; if (obj instanceof String) { String str = (String) obj; System.out.println("String length: " + str.length()); }
🌐
SoftwareMill
softwaremill.com › primitive-types-in-patterns-instanceof-and-switch-in-java-23
Primitive Types in Patterns, Instanceof, and Switch in Java 25 | SoftwareMill
September 15, 2025 - Explore how Java 25 introduces primitive types for instanceof and switch, simplifying code readability.
🌐
Claudiodesio
claudiodesio.com › home › blog › going beyond java 8: pattern matching for instanceof
Going beyond Java 8: pattern matching for instanceof - Claudio De Sio Cesari
June 23, 2022 - In this article we will see an interesting novelty introduced in version 14 as a preview feature (see related article), and definitively made official as a standard feature with Java 16.
Top answer
1 of 2
3

The way the quote explains why the second case does not compile is worded extremely strange, maybe even wrong. So here is my way of explaining it.

The way you have to look at pattern match variables is that they are only declared if they are assigned (in the spec they call this 'introducing' a pattern variable).

Let's look at an example:

if(myOb instanceof Integer iObj) {
    // ...
}

Now inside this if statement, the instanceof must have returned true, so iObj is 'introduced' and therefore declared. This means you can both read from it, and write to it (as it isn't a final variable, note that myOb instanceof final Integer iObj is valid Java).

Let's now jump to the first case given:

if((count < 100) && myOb instanceof Integer iObj) {
    // ...
}

So why does this work? The language designers made sure that if you have a statement in the form of:

if (exprA && exprB) {
  // ...
}

that it is equivalent to:

if (exprA) {
  if (exprB) {
    // ...
  }
}

This why you have short circuiting in Java and so many other languages. Doing this to the 1st case we get:

if (count < 100) {
  if (myOb instanceof Integer iObj) {
    // ...
  }
}

Because the inner if matches the first example I gave, it makes sense that again inside the if block the pattern variable must have been 'introduced' and is therefore assignable.

So why doesn't this work with & just like with &&? Well, because the language designers decided not to I suppose.

I do have an argument as to why they decided this. Given a statement like this:

if (exprA & exprB) {
  // ...
}

It is essentially the same as:

boolean tempA = exprA;
boolean tempB = exprB;
if (tempA && tempB) {
  // ...
}

And although we can split the && operator here again, it doesn't matter. In the case you gave, we would get boolean tempB = myOb instanceof Integer iObj. This statement can't 'introduce' a pattern variable because it could be that myOb wasn't an Integer which means that it can't guarantee that iObj would be assigned.

So even though 'myOb instanceof Integer iObj' in both cases must have returned true for the code in the if block to be run, because the language designers didn't make rules for how pattern variables get introduced by & like they did with &&, the 2nd case in the book won't compile like the first one.

So why can you use int iObj = ... in the if block with & but not with &&? Well, as we just saw with &, the compiler doesn't consider the pattern variable to be 'introduced' to the block, so you can define it inside, but with && that is illegal because the pattern variable IS introduced, which means you can't redeclare it.

2 of 2
0

First, the quote as presented in the question is crap!

The && is the "shortcut" form that does not guarantee that both sides will be executed; in this case, if the first term evaluates to false, the second one will never be touched. & guarantees that both terms are evaluated.

Nevertheless, it is possible that Pattern Matching does not work with single & for some reason (but not for that one given in the quote!!)

Pattern matching for instanceof was introduced in Java 14 as a preview, a second preview was started in Java 15, until it was finalised in Java 16. And the JEP talks in fact only about && and ||, not about & and |. But to have that behaving different than the shortcuts does not make much sense.

Meanwhile, I tried

if( (count < 100) & myOb instanceof Integer iObj ) 
{
   int iObj = (Integer) myOb;
}

with Java 21, and it works – unexpectedly! But when I use &&, it complains about the assignment/declaration in the body, because iObj was already declared … strange!!

But the JLS 6.3.1 also talk about the shortcuts only …

Perhaps JLS 15.22.2 gives the answer: there & and | are labeled as "boolean logical operators", while 6.3.1 talked about && and || as "Conditional-And" or "Conditional-Or" operators, respectively.

But then the question arises why the dangling iObj in the instanceof term with the single & does not cause a syntax error?

🌐
Medium
medium.com › @AlexanderObregon › how-javas-instanceof-operator-works-09071a27cd3b
How Java’s instanceof Operator Works | Medium
February 28, 2025 - Learn how Java’s instanceof operator checks object types at runtime, works with inheritance and interfaces, and how Java 14’s pattern matching improves type checks.
🌐
Todd Ginsberg
todd.ginsberg.com › post › java-16 › instanceof
Coming in Java 16: Pattern Matching for instanceof • Todd Ginsberg
January 12, 2021 - This is a much simpler version of what we would have had to do before, where we would have had an ugly manual cast: // Before pattern matching for instanceof return (someObject instanceof String) && ((String)someObject).startsWith("Awesome"); Think of how much nicer our equals methods are going to look! Let’s rewrite the equals method on java.lang.Integer:
🌐
InfoQ
infoq.com › news › 2020 › 08 › java16-records-instanceof
Records and Pattern Matching for Instanceof Finalized in JDK 16 - InfoQ
August 12, 2020 - Final releases of records and the new pattern matching functionality for instanceof are planned for JDK 16.
🌐
Programiz
programiz.com › java-programming › instanceof
Java instanceof (With Examples)
Here, we have used the instanceof operator to check whether name and obj are instances of the String and Main class respectively. And, the operator returns true in both cases. Note: In Java, String is a class rather than a primitive data type.
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › instanceof in java
Instance Variable in Java: Definition, Examples, and Key Differences
February 1, 2026 - Learn about Instance Variable in Java, its features, default values, and how it differs from static variables. Explore practical examples to enhance your Java programming skills.
🌐
W3Schools
w3schools.com › java › ref_keyword_instanceof.asp
Java instanceof Keyword
The instanceof keyword compares the instance with type. The return value is either true or false. Read more about objects in our Java Classes/Objects Tutorial.