48

プリミティブデータ型(PDT)変数の型またはオペランドPDTの式を返すJavaの「typeof」のような関数はありますか?

instanceofクラスタイプでのみ機能するようです。

4

4 に答える 4

76

次のことを試してください。

int i = 20;
float f = 20.2f;
System.out.println(((Object)i).getClass().getName());
System.out.println(((Object)f).getClass().getName());

印刷されます:

java.lang.Integer
java.lang.Float

に関してはinstanceof、動的な対応物を使用できますClass#isInstance

Integer.class.isInstance(20);  // true
Integer.class.isInstance(20f); // false
Integer.class.isInstance("s"); // false
于 2012-09-11T01:24:28.690 に答える
23

暗黙のボクシングを必要としない簡単な方法があるので、プリミティブとそのラッパーの間で混乱することはありません。プリミティブ型には使用できません。isInstanceたとえば、Integer.TYPE.isInstance(5)Integer.TYPEと同等のint.class)を呼び出すと、事前に自動ボックス化されたとおりに返されfalseます。5Integer

必要なものを取得する最も簡単な方法(注-プリミティブのコンパイル時に技術的に実行されますが、それでも引数の評価が必要です)はオーバーロードを使用することです。私のイデオネペーストを参照してください。

...

public static Class<Integer> typeof(final int expr) {
  return Integer.TYPE;
}

public static Class<Long> typeof(final long expr) {
  return Long.TYPE;
}

...

これは、たとえば次のように使用できます。

System.out.println(typeof(500 * 3 - 2)); /* int */
System.out.println(typeof(50 % 3L)); /* long */

これは、式のタイプを判別し、適切なオーバーロードを選択するコンパイラーの機能に依存しています。

于 2012-09-11T03:03:47.947 に答える
2

次のクラスを使用できます。

class TypeResolver
{
    public static String Long = "long";
    public static String Int = "int";
    public static String Float = "float";
    public static String Double = "double";
    public static String Char = "char";
    public static String Boolean = "boolean";
    public static String Short = "short";
    public static String Byte = "byte";

    public static void main(String[] args)
    {
        //all true
        TypeResolver resolver = new TypeResolver();
        System.out.println(resolver.getType(1) == TypeResolver.Int); 
        System.out.println(resolver.getType(1f) == TypeResolver.Float); 
        System.out.println(resolver.getType(1.0) == TypeResolver.Double);
        System.out.println(resolver.getType('a') == TypeResolver.Char); 
        System.out.println(resolver.getType((short) 1) == TypeResolver.Short); 
        System.out.println(resolver.getType((long) 1000) == TypeResolver.Long);
        System.out.println(resolver.getType(false) == TypeResolver.Boolean); 
        System.out.println(resolver.getType((byte) 2) == TypeResolver.Byte);
    }

    public String getType(int x)
    {
        return TypeResolver.Int;
    }

    public String getType(byte x)
    {
        return TypeResolver.Byte;
    }

    public String getType(float x)
    {
        return TypeResolver.Float;
    }

    public String getType(double x)
    {
        return TypeResolver.Double;
    }

    public String getType(boolean x)
    {
        return TypeResolver.Boolean;
    }

    public String getType(short x)
    {
        return TypeResolver.Short;
    }

    public String getType(long x)
    {
        return TypeResolver.Long;
    }

    public String getType(char x)
    {
        return TypeResolver.Char;
    }
}
于 2019-10-23T15:04:34.387 に答える
0

プリミティブ型の型を判別するために使用できる方法は2つあります。

package com.company;

public class Testing {
public static void main(String[] args) {
    int x;
    x=0;
    // the first method 
    System.out.println(((Object)x).getClass().getName());
    if (((Object)x).getClass().getName()=="java.lang.Integer")
        System.out.println("i am int");
   // the second method it will either return true or false
    System.out.println(Integer.class.isInstance(x));
}

}

于 2020-10-01T12:25:01.787 に答える