Java StringBuffer codePoints() Method



The Java StringBuffer codePoints() method is used to return the Unicode values representing the current object. Since the StringBuffer holds a sequence of values, this method will return a stream of code point values.

If this method encounters any surrogate pairs (combination of Unicode code point values in the range U+D800 to U+DBFF with other Unicode code point values in the range U+DC00 to U+DFFF) in the sequence, they are converted to their supplementary code point values using Character.toCodePoint() method and the result is passed to the integer stream.

Any other code units, including ordinary BMP (Basic Multilingual Plane) characters from U+0000 to U+FFFF, unpaired surrogates, and undefined code units, are zero-extended to int values which are then passed to the stream.

The stream binds to this sequence when the terminal stream operation starts (like when the first iteration begins). If the sequence is modified during that operation, then the result is undefined.

Syntax

Following is the syntax for the Java StringBuffer codePoints() method.

public IntStream codePoints()

Parameters

This method does not accept any parameters.

Return Value

This method returns an IntStream of Unicode code points from this sequence.

Example

When the method is invoked on a StringBuffer object, the result is the IntStream of the given sequence.

In this example, we are creating a StringBuffer object and initializing it with an input sequence of characters.

import java.lang.*;
import java.util.stream.*;

public class StringBuffercodePoints {
   public static void main(String[] args) {

      //create a  StringBuffer object
      StringBuffer obj = new StringBuffer("Tutorialspoint");
      IntStream s = obj.codePoints();
      //print the stream generated
      System.out.println("The IntStream Output is: " );
      s.forEach(System.out::print);
   }
}

Output

Let us compile and run the given program, to produce the following output −

The IntStream Output is: 
8411711611111410597108115112111105110116 

Example

In this example, when the method is invoked on a StringBuffer object with an input sequence of digits, the result is the IntStream of the given sequence.

import java.lang.*;
import java.util.stream.*;

public class StringBuffercodePoints {
   public static void main(String[] args) {

      //create a  StringBuffer object
      StringBuffer obj = new StringBuffer("123456");
      IntStream s = obj.codePoints();
      //print the stream generated
      System.out.println("The IntStream Output is: " );
      s.forEach(System.out::println);
   }
}

Output

Let us compile and run the given program, to produce the following output −

The IntStream Output is: 
49
50
51
52
53
54

Example

Similarly, when the method is invoked on a StringBuffer object with an input sequence of symbols, the result is the IntStream of the given sequence.

import java.lang.*;
import java.util.stream.*;

public class StringBuffercodePoints {
   public static void main(String[] args) {

      //create a  StringBuffer object
      StringBuffer obj = new StringBuffer("!@#$%^&*");
      IntStream s = obj.codePoints();
      //print the stream generated
      System.out.println("The IntStream Output is: " );
      s.forEach(System.out::println);
   }
}

Output

Let us compile and run the given program, to produce the following output −

The IntStream Output is: 
33
64
35
36
37
94
38
42 

Example

When the method is invoked on a StringBuffer object with an input sequence of codepoints using typecasting, the result is the IntStream of the given sequence.

import java.lang.*;
import java.util.stream.*;

public class StringBuffercodePoints {
   public static void main(String[] args) {

      //create a StringBuffer object
      StringBuffer obj = new StringBuffer(0xdc08);
      
      IntStream s = obj.codePoints();

      //print the stream generated
      System.out.println("The IntStream Output is: ");
      s.forEach(System.out::print);
   }
}

Output

Let us compile and run the given program, to produce the following output −

The IntStream Output is:

java_lang_stringbuffer.htm
Advertisements