Java - String chars() Method



The Java String chars() method returns a stream of integers zero-extending the char values from this sequence. If the given sequence is mutated while the stream is being read, the result is undefined.

The IntStream returned contains the character code point values from the given string. The chars() method does not accept any parameter. It throws an exception if the given string value is null.

Syntax

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

Public IntStream chars()

Parameters

  • It does not accept any parameter.

Return Value

It returns a stream of int zero-extending the char values from this sequence. Any character that corresponds to a surrogate code point is ignored.

Example

If the declared string does not contain the null value, the chars () method returns the IntStream value of the given string.

In the following program, we are creating a string literal with the value Hello world. Then, using the chars() method to print the Intstream of the given sequence.

import java.lang.*;
import java.util.stream.IntStream;
public class TP {
   public static void main(String[] args) {
      
      // TODO Auto-generated method stub
      //string declaration
      String str = "Hello World";
      System.out.println("The given string is: " + str);
      
      //using chars() method
      IntStream codePointStream = str.chars();
      System.out.println("The chars() method returns: ");
      codePointStream.mapToObj(Character::toChars).forEach(System.out::print);
   }
}

Output

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

The given string is: Hello World
The chars() method returns: 
Hello World

Example

If we initialize the string with the null value, this method throws the NullPointerException.

In this program, we are instantiating the string class with the value null. Using the chars() method, we are trying to print the IntStream value of the given string.

package com.tutorialspoint.String;
import java.lang.*;
import java.util.stream.IntStream;
public class Demo {
   public static void main(String[] args) {
      try {
         
         //create an object of the string
         String str = new String();
         str = null;
         System.out.println("The given string is: " + str);
         
         //using chars() method
         IntStream codePointStream = str.chars();
         System.out.println("The chars() method returns: ");         codePointStream.mapToObj(Character::toChars).forEach(System.out::print);
      } 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.chars()" because "str" is null
	at com.tutorialspoint.String.Demo.main(Demo.java:12)
Exception: java.lang.NullPointerException: Cannot invoke "String.chars()" because "str" is null

Example

If the declared string does not contain anything (empty value), this method does not return anything.

In the following example, we are creating string literals with an empty value, since the string is empty so the chars() method does not return anything.

package com.tutorialspoint.String;
import java.lang.*;
import java.util.stream.IntStream;
public class Demo {
   public static void main(String[] args)  throws Exception {
      
      //string declaration
      String str = "";//empty value
      System.out.println("The given string is an empty. " + str);
      
      //using chars() method
      IntStream codePointStream = str.chars();
      System.out.println("The chars() method returns: ");
      codePointStream.mapToObj(Character::toChars).forEach(System.out::print);
   }
}

Output

The above program, produces the following results −

The given string is an empty. 
The chars() method returns: 
java_lang_string.htm
Advertisements