为什么String类没有TYPE字段?

huangapple go评论94阅读模式
英文:

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&lt;?&gt; clazz = Boolean.TYPE; // (Class&lt;Boolean&gt;) Class.getPrimitiveClass(&quot;boolean&quot;); 

So, you can't do String.TYPE as it's not there.

You can do simply : Class&lt;?&gt; clazz = String.class;

huangapple
  • 本文由 发表于 2020年9月30日 10:11:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/64129939.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定