如何找出 System.out.print() 的类型

How would one go about finding out the type of System.out.print()

假设我已经设置了一些变量:

   byte a = 2;
   short b = 1;
   int c = 30;
   long d = 5;
   char e = 'E'; 
   float f = 2.21f;
   double g = 34.12;
   boolean h  = false;

我想找出以下计算的值:

(short)(g + a) != g == true || h && ! ( d % b >0) && d * f == e

所以我会选择:

System.out.println((short)(g + a) != g == true || h && ! ( d % b >0) && d * f == e;

这会 return true。我知道这很可能是一个布尔值。假设我想确定并有一个命令来打印 both 结果和类型我该怎么做?我希望它是一个可以反复复制的解决方案。

System.out.println() 严重超载。你可以用你自己的方法做同样的事情。

static void printValueAndType(int i) {
    System.out.println("" + i + " int");
}

static void printValueAndType(boolean b) {
    System.out.println("" + b + " boolean");
}

static void printValueAndType(Object obj) {
    System.out.println("" + obj + ' ' + obj.getClass().getName());
}

一共有8种基本类型:boolean, byte, short, char, int, long, float, double(你在问题的代码中已经提到了all)。您可能需要每个方法。

让我们试试看,为什么不呢?

    printValueAndType((short)(g + a) != g == true || h && ! ( d % b >0) && d * f == e);

输出:

true boolean