- Java.lang Package classes
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package extras
- Java.lang - Interfaces
- Java.lang - Errors
- Java.lang - Exceptions
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java StringBuffer chars() Method
The Java StringBuffer chars() method is used to map a StringBuffer to a stream of int zero-extending char values. Before we get further into this method, let us understand what the term “zero-extend” means −
As we already know, int data type is a 32-bit value and char is 16-bit; the term zero-extend quite literally means that all the higher order bits (or most significant bits) that would be left unused during conversion are assigned with zeroes.
However, if any char value is mapped with the surrogate code point, the char is not interpreted.
The binding of the StringBuffer to the int stream is done using a spliterator. A spliterator is an interface that traverses and splits through a collection of elements; as a StringBuffer is mutable, a late-binding spliterator binds the stream to the source of elements when the terminal stream operations are first performed (like during first traversal, first split or first query), and not when the spliterator is created. Therefore, if the sequence is modified during the binding operation, the result remains undefined.
Syntax
Following is the syntax for the Java StringBuffer chars() method
public IntStream chars()
Parameters
This method does not accept any parameters.
Return Value
The method returns an IntStream of char values from this sequence.
Example
For a simple character sequence with letters, the method returns the IntStream of these char values.
In the following example, let us see the usage of Java StringBuffer chars() method. Here, we are creating a StringBuffer object and initializing it with a char sequence. Then the method is invoked on it.
import java.lang.*; import java.util.stream.*; public class StringBufferChars { public static void main(String args[]) { //create a StringBuffer object and initialize it StringBuffer obj = new StringBuffer("Tutorialspoint"); //declare an InStream and store the return value in it IntStream s = obj.chars(); //print the int stream System.out.println("The int stream for StringBuffer - " + obj + " is: "); s.forEach(System.out::println); } }
Output
Let us compile and run the given program, to produce the following output −
The int stream for StringBuffer - Tutorialspoint is: 84 117 116 111 114 105 97 108 115 112 111 105 110 116
Example
In another example, if we initialize the StringBuffer object with digits as charsequence, the method returns the IntStream of these values.
import java.lang.*; import java.util.stream.*; public class StringBufferChars { public static void main(String args[]) { //create a StringBuffer object and initialize it StringBuffer obj = new StringBuffer("1832974829"); IntStream s = obj.chars(); //print the int stream System.out.println("The int stream for StringBuffer - " + obj + " is: "); s.forEach(System.out::print); } }
Output
Let us compile and run the program, to obtain the output as follows −
The int stream for StringBuffer - 1832974829 is: 49565150575552565057
Example
If we assign code points (both ordinary code points and surrogate code points) as values to the StringBuffer object, the method returns the IntStream for them.
import java.lang.*; import java.util.stream.*; public class StringBufferChars { public static void main(String args[]) { //create a StringBuffer object and initialize it with a codepoint StringBuffer obj1 = new StringBuffer("a"); //create another StringBuffer object and initialize it with a surrogate codepoint StringBuffer obj2 = new StringBuffer(0xdc01); IntStream s1 = obj1.chars(); IntStream s2 = obj2.chars(); //print the int stream for ordinary character System.out.println("The int stream for StringBuffer - " + obj1 + " is: "); s1.forEach(System.out::println); //print the int stream for surrogate code point System.out.println("The int stream for StringBuffer - " + obj2 + " is: "); s2.forEach(System.out::print); } }
Output
The output for the program above is −
The int stream for StringBuffer - a is: 97 The int stream for StringBuffer - is: