如何在 Java 中使用 instanceof 運算子?


instanceof 運算子僅用於物件引用變數。此運算子檢查物件是否是特定型別(類型別或介面型別)。

示例

public class Test {
   public static void main(String args[]) {
      String name = "James";
      boolean result = name instanceof String;
      System.out.println(result);
   }
}

輸出

true

更新於: 2020-02-20

190 次瀏覽

開啟你的職業生涯

完成課程,獲得認證

開始
廣告
© . All rights reserved.