英文:
Why String class doesn't have a TYPE field?
问题
在Java中,如果我想获取Class
对象,我可以使用TYPE属性。
例如:
Class<?> clazz = Boolean.TYPE;
但为什么String
类没有一个TYPE字段呢?
英文:
In Java, if I want to get Class
object, I can use TYPE attribute.
For example:
Class<?> clazz = Boolean.TYPE;
By why String
class doesn't have a TYPE field ?
答案1
得分: 3
字符串没有它,因为 String
不需要它:它将会与 String.class
完全相同,而获取它同样简单。区别在于 Boolean.TYPE
不同于 Boolean.class
,而是 boolean.class
;包装类与原始类型不同。
英文:
String doesn't have it because String
doesn't need it: it would just be exactly the same as String.class
, which is just as easy to get to. The difference is that Boolean.TYPE
is not the same as Boolean.class
, but rather boolean.class
; the wrapper class is different from the primitive.
答案2
得分: 1
TYPE属性表示Boolean
类中的原始类型boolean
。但是String
没有原始类型。您可以在任何类上使用getClass()
方法来获取类对象。请参考https://docs.oracle.com/javase/8/docs/api/java/lang/Boolean.html#TYPE。
英文:
TYPE attribute represents the primitive type boolean
in Boolean
class. But String
does not have a primitive type. You can use getClass()
method on any class to get the class object.
Refer
https://docs.oracle.com/javase/8/docs/api/java/lang/Boolean.html#TYPE
答案3
得分: 1
TYPE 静态成员变量对于 String
类不可用,因为它不是一个包装类。
基本上,它用于返回特定基本类型类的类实例。
例如:
Class<?> clazz = Boolean.TYPE; // (Class<Boolean>) Class.getPrimitiveClass("boolean");
所以,你不能使用 String.TYPE
,因为它不存在。
你可以简单地这样做:Class<?> clazz = String.class;
英文:
The TYPE static member variable is not available for String
class as it's not a wrapper class.
Basically, it is used to return class instance for a particular primitive type class.
For example :
Class<?> clazz = Boolean.TYPE; // (Class<Boolean>) Class.getPrimitiveClass("boolean");
So, you can't do String.TYPE
as it's not there.
You can do simply : Class<?> clazz = String.class;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论