instanceof的用法是确认他是否为该类型,返回boolean类型
语法:
(Object)值 instanceof 基础类的封装类
package com.cdz.test;
import java.util.Scanner;
public class Type {
public Object judgeType(Object temp) {
String type;
if(temp instanceof String)
return type="String";
if(temp instanceof Integer)
return type="Integer";
if(temp instanceof Boolean)
return type="Boolean";
if(temp instanceof Long)
return type="Long";
if(temp instanceof Short)
return type="Short";
if(temp instanceof Byte)
return type="Byte";
if(temp instanceof Character)
return type="Character";
if(temp instanceof Float)
return type="Float";
if(temp instanceof Double)
return type="Double";
if(temp.getClass().isArray()) //造价于temp instanceof int[]
return type="int[]";
// if(temp instanceof int[])
// return type="int[]";
return null;
}
public static void main (String[] args) {
Type type=new Type();
int a[]=new int[10]; //测试数组类型
Object value='a'; //改变值
System.out.println(type.judgeType(value));
}
}
注:上面数组的类型判断时的第二种方法,只能是,是什么类型,就判断什么类型