Sunday, 8 February 2015

What is the importance of "Charsequence" in java


Java CharSequence is an interface. As the API says, CharSequence has been implemented in CharBufferSegmentStringStringBufferStringBuilder classes. So if you want to access or accept your API from all these classes thenCharSequence is your choice. If not then String is very good for a public API because it is very easy & everybody knows about it. Remember CharSequence only gives you 4 method, so if you are accepting a CharSequence object through a method, then your input manipulation ability will be limited.
-----------------------------------------------------------------------------------------------------
It's just re factored 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 StringCharBufferStringBuilderStringBuffer and other CharSequence implementations in.
This fact has however nothing to do with java.util.Regex, it only takes benefit of it =)
--------------------------------------------------------------------------------------------------
for more read:-
stackoverflow:-

No comments:

Post a Comment