Java中的Name接口有什么需求?

What is the need for Name interface in Java?

在Java中,接口Name (which extends CharSequence)什么时候有用?为什么不直接使用 final String 呢?我没有看到任何实现 Name.

的 classes

另外,我感兴趣的是 Name, CharSequence, and the String class.

的用例

它不仅仅是一个字符串。

equals 方法的描述中说:

Note that the identity of a Name is a function both of its content in terms of a sequence of characters as well as the implementation which created it.

所以这意味着实现 Name 的对象不仅仅是其字符表示。它还必须了解创建它的实现。

如果您只想比较必须使用的内容 contentEquals

但您可能不会自己使用此界面创建 类。它是 Java 语言模型的一部分。

根据评论总结答案:@Mark Rotteveel

This is part of the package java.lang.model.element which is "Interfaces used to model elements of the Java programming language.". In other words, it is not general purpose. And there are implementations, but just not in a public API as these interfaces are the API (e.g. in Temurin 11, there are
com.sun.tools.javac.util.Name,
com.sun.tools.javac.util.SharedNameTable.NameImpl and
com.sun.tools.javac.util.UnsharedNameTable.NameImpl).

而且它不仅仅是基于@rghome 答案的字符串。

此外,模型也以相同的语言提供的原因是:@Zabuzard

It is mostly for the meta-aspects that Java provides, in particular the reflection API that allows you to inspect the code and retrieve meta-information about it, or to dynamically trigger stuff. For example call a method by its name, given by user input.

CharSequence 是一个非常通用的 接口 ,比 String 更通用,并且与 String 的普遍用法相反使用 CharSequence 代替,因此也可以传递 StringBuilder。

然而 pro String (a class) 计算其明显的不变性和额外的方法。尽管接口 CharSequence 本身也没有可变函数。由于只有 StringBuilderString 是最有用的实现,所以通常人们可能会忘记使用 CharSequence.

Name 是语言建模本身的专用 接口 。它是一种将值类型(此处为String)包装在其自身类型中以供特定用途的一种形式。就像将时间字符串包装在 Time 中一样。请注意,对于 case-insensitive 语言,Name#equals 可以忽略大小写(如 PascalName implements Name)。 class/interface 不应在语言建模之外使用。在模型中,名称可能包含其声明。