Java String codePoints() method



The Java String codePoints() method is used to return a stream of code point(Unicode) values from this sequence. The code point values are similar to the ASCII value in Java. The ASCII value stands on the American Standard Code for information Interchange, it is an integer value of the characters. It lies between the range (0-127).

The codePoints() method does not accept any parameter, it throws an exception if the given string value is null.

Note: Java actually uses Unicode, which includes ASCII and other characters from languages around the world.

Syntax

Following is the syntax of the Java String codePoints() method −

Public IntStream codePoints()

Parameters

  • It does not accept any parameter

Return Value

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

Example

If the given string value is not null, the codePoints() method returns the code point (Unicode) values of all the characters in it.

In the following program, we are instantiating the string class with the value Java.Using the codePoints() method, we are trying to print the code point values of all the characters of the given string.

import java.lang.*;
import java.util.stream.IntStream;
public class Demo {
   private static final String IntStream = null;
   public static void main(String[] args) {
      
      //instantiate the string class
      String str = new String("Java");
      System.out.println("The given string is: " + str);
      
      //using codePoints() method
      IntStream codePoint = str.codePoints();
      System.out.println("The code point values of all-character are: ");
      codePoint.forEach(System.out::println);
   }
} 

Output

On executing the above program, it will produce the following result −

The given string is: Java
The code point values of all-character are: 
74
97
118
97

Example

If the given string value is null, the codePoints() method throws a NullPointerException.

In this example, we are creating a string of literals with the value null. Then, using the codePoints() method, we are trying to retrieve the character code point value of the given string. Since the string value is null, an exception is thrown.

import java.lang.*;
import java.util.stream.IntStream;
public class Test {
   public static void main(String[] args) {
      try {
         
         //create string literals
         String str = null;
         System.out.println("The given string is: " + str);
         
         //using codePoints() method
         IntStream codePoint = str.codePoints();
         System.out.print("The character code point(Unicode) values are: ");
         codePoint.forEach(System.out::println);
      } catch(NullPointerException e) {
         e.printStackTrace();
         System.out.println("Exception: " + e);
      }
   }
}

Output

Following is the output of the above program −

The given string is: null
java.lang.NullPointerException: Cannot invoke "String.codePoints()" because "str" is null
Exception: java.lang.NullPointerException: Cannot invoke "String.codePoints()" because "str" is null
	at com.tutorialspoint.String.Test.main(Test.java:11)
java_lang_string.htm
Advertisements