判断变量类型

 1 public class ClassType {
 2     public static void main(String[] args) {
 3         Integer i=0;
 4         String type = ifClassType(i);
 5         System.out.println(type);
 6     }
 7     
 8     public static String ifClassType(Object obj) {
 9         String type="";
10         if(obj instanceof Integer){
11             type = "Integer";
12         }else if(obj instanceof Short){
13             type = "Short";
14         }else if(obj instanceof Double){
15             type = "Double";
16         }else if(obj instanceof Long){
17             type = "Long";
18         }else if(obj instanceof String){
19             type = "String";
20         }else if(obj instanceof Boolean){
21             type = "Boolean";
22         }else if(obj instanceof Character){
23             type = "Character";
24         }else if(obj instanceof T){//自定义类型
25             type = "T";
26         }
27         return type;
28     }
29     class T{}
30 }

 

posted @ 2013-07-24 11:39  小嫩芽儿  阅读(237)  评论(0编辑  收藏  举报