Java簡單類型判斷
阿新 • • 發佈:2019-02-01
ati getc clas 類型 ret else version col character
package com; /** * Java類型判斷,工具類 * * @author LinXu * */ public class TypeIns { public static void main(String[] args) { Object object = 9;// Object類 System.out.println("是否是String:" + TypeConversion.isString(object));// 判斷是否是|String類型 System.out.println("是否是Int:" + TypeConversion.isInt(object)); System.out.println("獲取當前Object類型:" + TypeConversion.getClassType(object)); Class<?> clazz = TypeConversion.getClassType(object); if (TypeConversion.isInt(clazz)) {// 判斷是否是int System.out.println("是Integer類型"); } else if (TypeConversion.isString(object)) {// 判斷是否是string System.out.println("是String類型"); } } }class TypeConversion<T> { public static <T> boolean isString(T t) { return t instanceof String; } public static <T> boolean isByte(T t) { return t instanceof Byte; } public static <T> boolean isShort(T t) { return t instanceof Short; }public static <T> boolean isInt(T t) { return t instanceof Integer; } public static <T> boolean isLong(T t) { return t instanceof Long; } public static <T> boolean isChar(T t) { return t instanceof Character; } public static <T> boolean isFloat(T t) { return t instanceof Float; } public static <T> boolean isDouble(T t) { return t instanceof Double; } public static <T> boolean isBytes(T t) { return t instanceof Byte; } public static <T> Class<?> getClassType(T t) { return t.getClass(); } } 是否是String:false 是否是Int:true 獲取當前Object類型:class java.lang.Integer
Java簡單類型判斷