Skip to main content

CharSequence interface in java

java.lang.CharSequence is an interface in the Java programming language that represents a sequence of characters. It defines a set of methods for accessing the characters in a string or other character sequence, without specifying the underlying implementation.

The CharSequence interface is implemented by many classes in the Java Standard Library, including String, StringBuilder, StringBuffer, and CharBuffer. It provides a uniform way to work with character sequences, regardless of the specific implementation.

Some of the methods defined by the CharSequence interface include charAt(), length(), and subSequence(), which allow you to access and manipulate the characters in a character sequence. These methods are used extensively in Java programming for tasks such as searching, sorting, and manipulating strings.

One advantage of using the CharSequence interface is that it allows your code to be more flexible and reusable, since it can work with any class that implements the interface. It also makes your code more robust, since you can change the underlying implementation of a character sequence without affecting the code that uses it, as long as the new implementation also implements the CharSequence interface.

Inside of this interface exists 6 public methods. Declaration of this interface as follows.

public interface java.lang.CharSequence {
  public abstract int length();
  public abstract char charAt(int);
  public abstract java.lang.CharSequence subSequence(int, int);
  public abstract java.lang.String toString();
  public java.util.stream.IntStream chars();
  public java.util.stream.IntStream codePoints();
  private java.util.Spliterator$OfInt lambda$codePoints$1();
  private java.util.Spliterator$OfInt lambda$chars$0();
}

CharSequence public method

There are following useful methods which is define the inside of java.lang.CharSequence java interface.

MethodDescription
char charAt(int index) It returns the value at the specified index.
default IntStream chars() It returns a stream of zero-extending the char values from this sequence.
default IntStream codePoints() It returns a stream of code point values from this sequence.
int length() It returns the length of this character sequence.
CharSequence subSequence(int start,int end) It returns a that is a subsequence of this sequence.
String toString() It returns a string containing the characters in this sequence in the same order as this sequence.

This reference is belong to javadoc

Implementing Classes

There are following java class are implements this interface functionality.

Such as CharBuffer, Segment, String, StringBuffer, StringBuilder. Here given few example.

// Using of String Class
// Useful packages
import java.lang.CharSequence;
import java.lang.String;
class Example
{
	public static void main(String[] args)
	{
		CharSequence x = new String("ABCDE");
		// Interface method  
		System.out.println(x.toString());
		System.out.println(x.charAt(0));
		System.out.println(x.length());
		System.out.println(x.subSequence(0, 2));
		// IntStream of Unicode code points from this sequence
		System.out.println(x.codePoints());
		// IntStream of char values from this sequence
		System.out.println(x.chars());
	}
}
CharSequence using String class
ABCDE
A
5
AB
java.util.stream.IntPipeline$Head@816f27d
java.util.stream.IntPipeline$Head@3e3abc88
// Using of javax.swing.text.Segment
// Useful packages
import java.lang.CharSequence;
import javax.swing.text.Segment;
class Example
{
	public static void main(String[] args)
	{
		char[] arr = {
			'A' , 'B' , 'C' , 'D' , 'E'
		};
		CharSequence x = new Segment(arr, 0, 3);
		// Interface method  
		System.out.println(x.toString());
		System.out.println(x.charAt(0));
		System.out.println(x.length());
		System.out.println(x.subSequence(0, 2));
		// IntStream of Unicode code points from this sequence
		System.out.println(x.codePoints());
		// IntStream of char values from this sequence
		System.out.println(x.chars());
	}
}
CharSequence using Segment class
ABC
A
3
AB
java.util.stream.IntPipeline$Head@816f27d
java.util.stream.IntPipeline$Head@3e3abc88
// Using of StringBuffer Class
// Useful packages
import java.lang.CharSequence;
import java.lang.String;
class Example
{
	public static void main(String[] args)
	{
		CharSequence x = new StringBuffer("ABCDE");
		// Interface method  
		System.out.println(x.toString());
		System.out.println(x.charAt(0));
		System.out.println(x.length());
		System.out.println(x.subSequence(0, 2));
		// IntStream of Unicode code points from this sequence
		System.out.println(x.codePoints());
		// IntStream of char values from this sequence
		System.out.println(x.chars());
	}
}
CharSequence using StringBuffer class
ABCDE
A
5
AB
java.util.stream.IntPipeline$Head@816f27d
java.util.stream.IntPipeline$Head@3e3abc88
// Using of StringBuilder Class
// Useful packages
import java.lang.CharSequence;
import java.lang.StringBuilder;
class Example
{
	public static void main(String[] args)
	{
		CharSequence x = new StringBuilder("ABCDE");
		// Interface method  
		System.out.println(x.toString());
		System.out.println(x.charAt(0));
		System.out.println(x.length());
		System.out.println(x.subSequence(0, 2));
		// IntStream of Unicode code points from this sequence
		System.out.println(x.codePoints());
		// IntStream of char values from this sequence
		System.out.println(x.chars());
	}
}
CharSequence using StringBuilder class
ABCDE
A
5
AB
java.util.stream.IntPipeline$Head@816f27d
java.util.stream.IntPipeline$Head@3e3abc88
// Using of CharBuffer
import java.nio.CharBuffer;
public class Example
{
	public static void main(String[] args)
	{
		try
		{
			CharBuffer x = CharBuffer.allocate(5);
			x.append("ABC");
			x.append('D');
			x.append('E');
			x.rewind();
            // Test the method
			System.out.println(x.toString());
			System.out.println(x.charAt(0));
			System.out.println(x.length());
			System.out.println(x.subSequence(0, 2));
			// IntStream of Unicode code points from this sequence
			System.out.println(x.codePoints());
			// IntStream of char values from this sequence
			System.out.println(x.chars());
		}
		catch (Exception e)
		{
			System.out.println(e);
		}
	}
}
CharSequence using CharBuffer class
ABCDE
A
5
AB
java.util.stream.IntPipeline$Head@816f27d
java.util.stream.IntPipeline$Head@6ce253f1




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment