详细阐述 Java 中 operator 的实例的合法操作数?
Java 中的instanceof 操作符用于查找引用是否是 Type 的实例,即类或接口。
示例
public class InstanceOfExample {
public static void main(String args[]) {
String str = "hello";
boolean bool = str instanceof String;
System.out.println(bool);
}
}输出
true
instanceof 操作符的合法操作数
以下是instanceof 操作符唯一的合法操作数 -
- 左操作数 - 必须是表示对象的引用。
- 右操作数 - 必须是 Java 类或接口的名称。
除这两个操作数之外,如果您使用任何其他操作数,将生成编译时错误。
public class InstanceOfExample {
public static void main(String args[]) {
int i =20;
boolean bool = i instanceof String;
System.out.println(bool);
}
}编译时错误
InstanceOfExample.java:4: error: unexpected type
boolean bool = i instanceof String;
^
required: reference
found: int
1 error
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP