CharSequence Interface in Java

By: Riktesh Srivastava 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++)
class program {
public static void main(String args[])
String s = \"test\";
A a=new A();
// StringBuffer
StringBuffer sb = new StringBuffer(\"ing\");

Author URL:

Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial