Hi,
Why is CharSequence from the Java API an interface? What is the significance of this interface?
Hi,
Why is CharSequence from the Java API an interface? What is the significance of this interface?
It's just refactored from any existing implementations. One of the benefits is that you can "widen" the input whenever you actually only need one of its methods.
So instead of for example
public void printEveryChar(String string) {
for (int i = 0; i < string.length(); i++) {
System.out.println(string.charAt(i));
}
}
you can have
public void printEveryChar(CharSequence charSequence) {
for (int i = 0; i < charSequence.length(); i++) {
System.out.println(charSequence.charAt(i));
}
}
so that you can pass String, CharBuffer, StringBuilder, StringBuffer and other CharSequence implementations in.
This fact has however nothing to do with java.util.Regex, it only takes benefit of it =)