在我的应用程序中,如果可能的话,我想打印出变量的值及其名称。
例如:
int testinteger = 0;
String type = > type of testinteger? (int);
String name = > name of testinteger? (testinteger);
System.out.println("The "+type+" "+name+" has a value of"+ testinteger);
所以我想读: int testinteger 的值为 0
不幸的是,我想,对于原始类型来说这是不可能的。但像 String
这样的类型也是如此吗? , Integer
, Boolean
ETC。?有可能得到吗?
最佳答案
你真的不能这样做;但是,您可以使用 Map<String,String>
以非动态方式做到这一点。键将是类型+变量名称(您必须对它们进行硬编码),值将是与这些变量关联的实际值(当然是字符串形式)。
关于java - 可以在java中获取变量的名称和/或类型吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16744876/