In Java, when is the interface Name (which extends CharSequence) useful? Why not just use final String
instead? I do not see any classes that implement Name
.
Alternatively, what I am interested is the use cases of Name, CharSequence, and the String class.
It is more than just a String.
In the description of the equals
method it says:
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.
So this implies that there is more to an object that implements Name
than just its character representation. It also has to know about the implementation which created it.
If you want to just compare the content you have to use contentEquals
.
But you are probably not going to be creating classes using this interface yourself. It is part of the Java language model.