Character.toChars(): This method is available in java.lang.Character class of Java.
Approach 1: When the method takes one argument.
Syntax:
char[] java.lang.Character.toChars(int codePoint)
This method takes one argument of type int as its parameter. This method converts the specified character (Unicode code point) to itsUTF-16 representation stored in a char array.
Parameters: One parameter is required for this method.
codePoint: a Unicode code point.
Returns: a char array having codePoint's UTF-16 representation.
Throws:
IllegalArgumentException - if the specified codePoint is not a valid Unicode code point.
Java
import java.util.Arrays;public class ChraractertoChars1 {public static void main(String[] args) {int codePoint = 124;System.out.println(Arrays.toString(Character.toChars(codePoint)));}}
Output:
[|]
Approach 1.1: IllegalArgumentException
Java
import java.util.Arrays;public class ChraractertoChars1 {public static void main(String[] args) {int codePoint = -1;System.out.println(Arrays.toString(Character.toChars(codePoint)));}}
Output:
Exception in thread "main" java.lang.IllegalArgumentException: Not a valid Unicode code point: 0xFFFFFFFF at java.base/java.lang.Character.toChars(Character.java:9175)
Approach 2: When the method takes three arguments.
Syntax:
int java.lang.Character.toChars(int codePoint, char[] dst, int dstIndex)
This method takes three arguments, one of type char array and the other two are of type int. This method converts the specified character (Unicode code point) to itsUTF-16 representation.
Parameters: Three parameters are required for this method.
codePoint: the character (Unicode code point) to be converted.
dst: an array of char in which the codePoint's UTF-16 value is stored.
dstIndex: the start index into the dst array where the converted value is stored.
Returns: 1 if the code point is a BMP code point, 2 if thecode point is a supplementary code point
Throws:
1. IllegalArgumentException - if the specified codePoint is not a valid Unicode code point.
2. NullPointerException - if the specified dst is null.
3. IndexOutOfBoundsException - if dstIndex is negative or not less than dst.length, or if dst at dstIndex doesn't have enough array element(s) to store the resulting char value(s). (If dstIndex is equal to dst.length-1 and the specified codePoint is a supplementary character, the high-surrogate value is not stored in dst[dstIndex].)
Java
public class CharactertoChars2 {public static void main(String[] args) {int codePoint = 1234;char[] dst = { 'H', 'e', 'l', 'l', 'o' };int dstIndex = 0;System.out.println(Character.toChars(codePoint, dst, dstIndex));}}
Output:
1
Approach 2.1: IllegalArgumentException
Java
public class CharactertoChars2 {public static void main(String[] args) {int codePoint = -1;char[] dst = { 'H', 'e', 'l', 'l', 'o' };int dstIndex = 0;System.out.println(Character.toChars(codePoint, dst, dstIndex));}}
Output:
Exception in thread "main" java.lang.IllegalArgumentException: Not a valid Unicode code point: 0xFFFFFFFF at java.base/java.lang.Character.toChars(Character.java:9146)
Approach 2.2: NullPointerException
Java
public class CharactertoChars2 {public static void main(String[] args) {int codePoint = 1234;char[] dst = null;int dstIndex = 0;System.out.println(Character.toChars(codePoint, dst, dstIndex));}}
Output:
Exception in thread "main" java.lang.NullPointerException: Cannot store to char array because "dst" is null at java.base/java.lang.Character.toChars(Character.java:9139)
Approach 2.3 : IndexOutOfBoundsException
Java
public class CharactertoChars2 {public static void main(String[] args) {int codePoint = 1234;char[] dst = { 'H', 'e', 'l', 'l', 'o' };int dstIndex = -1;System.out.println(Character.toChars(codePoint, dst, dstIndex));}}
Output:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 5 at java.base/java.lang.Character.toChars(Character.java:9139)
No comments:
Post a Comment