Flowscoping in Java 21Java

Java-Forum
Anonymous
 Flowscoping in Java 21

Post by Anonymous »

Da ich mich auf OCPJP 21 vorbereite, stieß ich auf Flow Scoping -Thema. Br />public class Question28
{

static void getFish(Object fish) {

if(!(fish instanceof String guppy))
System.out.println("Eat!");
else if(!(fish instanceof String guppy)) {
throw new RuntimeException();

}

System.out.println(guppy);

System.out.println("Swim!");
}

public static void main(String args[]) {

getFish("ashish");

}

}

< /code>
Über den Snippet führt zu "Symbol finden nicht" für variable Guppy. Guppy Variable bleibt im Bereich. Kein Compiler -Fehler. < /p>
static void getFish(Object fish) {

if(!(fish instanceof String guppy))
System.out.println("Eat!");
if(!(fish instanceof String guppy)) {
throw new RuntimeException();

}

System.out.println(guppy);
< /code>
Meine zweite Beobachtung wie folgt
Ref Snippet < /p>
class Sample2 {

static void printNumberTwice(Number number) {

if(!(number instanceof Integer data))
return ;
//System.out.println("Eat!");

System.out.println(data.intValue());

}

public static void main(String args[]) {

printNumberTwice(5);
}

}
< /code>
Über den Snippet erfolgreich kompiliert, aber wenn ich die Rückgabe kommentiere und stattdessen eine SOP -Anweisung hinzufüge, kann es ein Symbol für Daten finden. das Konzept verstehen.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post