CharSequence Interface in Java

By: Riktesh Srivastava Viewed: 153883 times  Printer Friendly Format    


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



Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial