CharSequence Interface in Java

By: Riktesh Srivastava  

This article explains in detail about CharSequence interface in detail. The interface was recently introduced as a part of JDK 1.4. The article explains this interface in detail using a simple example.

In Java 1.4, both the String and the StringBuffer classes implements java.lang.CharSequence interface, which is a standard interface for querying the length of and extracting characters and subsequences from a readable sequence of characters.  It can be explained with the help of example given below:

class A
{
public void dumpSeq(CharSequence cs)
{
System.out.println(\"length = \" + cs.length());
System.out.println(\"first char = \" + cs.charAt(0));
System.out.println(\"string = \" + cs);

int numChars = cs.length();
for(int i = 0; i < numChars; i++)
{
System.out.println(\"subsequence:\"+cs.subSequence(0,i+1));
}
}
}
class program {
public static void main(String args[])
{
String s = \"test\";
A a=new A();
a.dumpSeq(s);
// StringBuffer
StringBuffer sb = new StringBuffer(\"ing\");
a.dumpSeq(sb);
}
}

Author URL: www.rikteshjava.blogspot.com




Archived Comments

1. Asking questions are really fastidious thing if you are not understanding something completely, but
View Tutorial          By: geschenkefuermaenner.info at 2017-04-19 00:49:22

2. Debrakerne
View Tutorial          By: Debrakerne at 2017-03-16 11:20:10


Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial