zoukankan      html  css  js  c++  java
  • JAVA中如何取得一个变量的类型

    class Test 
    {
    public static void main(String[] args) 
    {
    int i=1;
    System.out.println(getType(i));
    }
    public static String getType(Object o){
    return o.getClass().toString();
    }
    }

    public class Test15 {
    public static void main(String[] args) 
    {
    int i=1;
    Integer i1 = 2;
    double d = 1.8;
    long l = 76;
    boolean b = false;
    
    System.out.println(getType(i));
    System.out.println(getType(i1));
    System.out.println(getType(d));
    System.out.println(getType(l));
    System.out.println(getType(b));
    }
    public static String getType(Object o){
    return o.getClass().toString();
    }
    public static String getType(int o){
    return "int";
    }
    public static String getType(byte o){
    return "byte";
    }
    public static String getType(char o){
    return "char";
    }
    public static String getType(double o){
    return "double";
    }
    public static String getType(float o){
    return "float";
    }
    public static String getType(long o){
    return "long";
    }
    public static String getType(boolean o){
    return "boolean";
    }
    public static String getType(short o){
    return "short";
    }
    }

    obj.getClass().getName()   ===〉    java.lang.Integer
    obj.getClass().toString()  ===〉    class java.lang.Integer

    public static void getType(Object object) {
    int length = object.getClass().getName().lastIndexOf(".");
    String type = object.getClass().getName().substring(length + 1);
    System.out.println(type);
    }
    基本类型不能得到

  • 相关阅读:
    [LeetCode]题解(python):119-Pascal's Triangle II
    [LeetCode]题解(python):118-Pascal's Triangle
    [LeetCode]题解(python):117-Populating Next Right Pointers in Each Node II
    寒假自学进度8
    寒假自学进度7
    寒假自学进度6
    寒假自学5
    寒假自学学习4
    寒假自学进度3
    寒假自学进度2
  • 原文地址:https://www.cnblogs.com/timssd/p/10298779.html
Copyright © 2011-2022 走看看