-3

There is such a method:

static void test(List<String> list) {
    System.out.println(list);
    if (!(list instanceof ArrayList<String> arrayList)) return;
    System.out.println(list == arrayList);
    arrayList.add("list");
    System.out.println(arrayList);
    System.out.println(list);
}

Output:

[]
true
[list]
[list]

Please explain how this is possible?

How does this code create another object (arrayList) that is available throughout the method?: list instanceof ArrayList<String> arrayList

Java 17.

luk2302
  • 55,258
  • 23
  • 97
  • 137
Leonis
  • 294
  • 3
  • 10
  • `list` and `arrayList` refer/point to the same object. – Andrew S Jun 16 '23 at 14:12
  • 1
    Explain how what exactly is possible? What is confusing you about that code? – OH GOD SPIDERS Jun 16 '23 at 14:13
  • I understand it. Please explain what is happening in this line: if (!(list instanceof ArrayList arrayList)) – Leonis Jun 16 '23 at 14:14
  • `List` is an interface, `ArrayList` is the implementation. So `instanceof` is checking if the implementation of `list` is an `ArrayList`. (The generic type might not matter because of erasure.) – Andrew S Jun 16 '23 at 14:15
  • How does this code create another object (arrayList)?: list instanceof ArrayList arrayList – Leonis Jun 16 '23 at 14:17
  • Not sure what you mean - that is the syntax. – Andrew S Jun 16 '23 at 14:19
  • Where can I read about this syntax? I see this for the first time – Leonis Jun 16 '23 at 14:19
  • 3
    I'm gonna take a gues here and guess you are asking for an explanation of the new instanceof syntax introduced with java 14: [Pattern Matching for instanceof in Java 14](https://blogs.oracle.com/javamagazine/post/pattern-matching-for-instanceof-in-java-14) – OH GOD SPIDERS Jun 16 '23 at 14:20
  • 2
    Pattern Matching for Instanceof releasaed with Java 16: [JEP394](https://openjdk.org/jeps/394) – user16320675 Jun 16 '23 at 14:22
  • Thanks, didn't know about this update in Java. – Leonis Jun 16 '23 at 14:23
  • https://stackoverflow.com/questions/5579309/is-it-possible-to-use-the-instanceof-operator-in-a-switch-statement/57432241#57432241 – A_Arnold Jun 16 '23 at 14:24
  • 1
    It can be helpful, it's somewhat strange as having a local variable presence. It doesn't appear correct, in terms of typical Java syntax. You would think it would only apply to the _if_ statement's scope; similar to _for-loops_. – Reilas Jun 16 '23 at 15:12

1 Answers1

5

This feature is called pattern matching and was introduced in Java 14 (see JEP 305) and finalized in Java 16 (see JEP 394).

A statement like if (list instanceof ArrayList<String> a) { ... } causes the list variable to be checked whether it is an instance of the ArrayList type, and if it is, then assign its value to a. Now a is available within the if branch.

Equivalent code without the pattern matching feature would look like something like this:

if (list instanceof ArrayList) {
    ArrayList<String> a = (ArrayList<String>) list;
    // do something with a
}

The compiler checks the conditions of the if statement to make sure the pattern matching variable is available in the correct scope. For example, your code contains a negation of the result of the instanceof operator (using the !):

if (!(list instanceof ArrayList<String> arrayList)) {
    return;
}
// Rest of the code

Now the compiler deduces that from the // Rest of the code line, list must be of type ArrayList<Integer>, so the variable arrayList is in scope from here.

More info

MC Emperor
  • 22,334
  • 15
  • 80
  • 130