public class testinstanceof
{
public static void main(String[] args)
{
Object hello="hello";//hello 的实际类型是String
System.out.println("字符串是否是Object类的实例:" + (hello instanceof Object));
System.out.println("字符串是否是String类的实例:" + (hello instanceof String));
System.out.println("字符串是否为Math类的实例:" + (hello instanceof Math));
System.out.println("字符串是否为Comparable接口的实例:" + (hello instanceof Comparable));
}
}
两个问题 1 第三个输出为什么是 false ,按理说hello是Object类,是Math的父类应该是true的。
2 第二个输出显示错误 拿错了?
{
public static void main(String[] args)
{
Object hello="hello";//hello 的实际类型是String
System.out.println("字符串是否是Object类的实例:" + (hello instanceof Object));
System.out.println("字符串是否是String类的实例:" + (hello instanceof String));
System.out.println("字符串是否为Math类的实例:" + (hello instanceof Math));
System.out.println("字符串是否为Comparable接口的实例:" + (hello instanceof Comparable));
}
}
两个问题 1 第三个输出为什么是 false ,按理说hello是Object类,是Math的父类应该是true的。
2 第二个输出显示错误 拿错了?
2.System.out.println("字符串是否是String类的实例:" + (hello instanceof String));
就没问题了