public static void main(String[] args) {
typedOf("123");
typedOf(true);
typedOf(1);
typedOf(1.1);
}
public static void typedOf(Object obj){
if (obj instanceof String) {
System.out.println("obj is String");
}else if(obj instanceof Boolean) {
System.out.println("obj is Boolean");
}else if(obj instanceof Integer) {
System.out.println("obj is Integer");
}else{
System.out.println("其他類型");
}
}
結果:
obj is String
obj is Boolean
obj is Integer
其他類型