wanggungun
  1. 基础数据类型没有方法能直接获取变量的类型,需要自定义一个函数,比如上面自定义的getType函数,通过getClass().getName()返回变量类型
  2. 包装类型的变量,可以通过getClass().getName()直接返回变量类型
public class Demo03 {

    public static String getType(Object o){
        return o.getClass().getName();
    }

    public static void main(String[] args) {
        int a = 10;
        String b = "hello";
        /*
        1. 基础数据类型没有方法能直接获取变量的类型,需要自定义一个函数,比如上面自定义的getType函数,通过getClass().getName()返回变量类型
        2. 包装类型的变量,可以通过getClass().getName()直接返回变量类型
        * */
        //System.out.println(a.getClass().getName());语法报错,a变量类型没有getClass()方法
        System.out.println(getType(a));//java.lang.Integer
        System.out.println(b.getClass().getName());//java.lang.String
    }
}

分类:

技术点:

相关文章:

  • 2021-11-14
  • 2021-06-23
  • 2021-10-06
  • 2021-12-09
  • 2021-08-30
  • 2021-11-21
  • 2020-06-15
猜你喜欢
  • 2021-12-19
  • 2020-07-13
  • 2021-12-19
  • 2021-07-23
  • 2021-09-28
  • 2021-01-14
  • 2021-12-19
相关资源
相似解决方案