码迷,mamicode.com
首页 > 编程语言 > 详细

Java简单类型判断

时间:2019-02-01 13:04:11      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:type   工具类   false   object   etc   ring   object类   char   stat   

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简单类型判断

标签:type   工具类   false   object   etc   ring   object类   char   stat   

原文地址:https://www.cnblogs.com/mature1021/p/10345402.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!