Java StringBuilder appendCodePoint() Method



The Java StringBuilder appendCodePoint() method appends the string representation of the code point argument to a sequence. In simple words, the argument accepted by this method is combined with the contents of a sequence to form a new string. The length of this sequence increases by the result of the Character.charCount(codePoint) method.

The overall effect of this method is exactly as if the argument were converted to a char array by the method Character.toChars(int) and the character in that array were then appended to this character sequence.

Syntax

Following is the syntax for the Java StringBuilder appendCodePoint() method

public StringBuilder appendCodePoint(int codePoint)

Parameters

  • codePoint − a Unicode code point

Return Value

The method returns the reference to this StringBuilder object.

Example: Appending String representation of CodePoint to StringBuilder

In the following example, when we simply pass a Unicode code point as an argument to this method, the character of this code point is appended to the input sequence and returned.


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

      StringBuilder word = new StringBuilder("Tutorialspoin");
      int cp = 116;

      System.out.println("The input sequence: " + word);
      System.out.println("The unicode code point: " + cp);
      System.out.println("The appended sequence: " + word.appendCodePoint(cp));
   }
}

Output

On compiling and executing the given program, the output is displayed as follows −

The input sequence: Tutorialspoin
The unicode code point: 116
The appended sequence: Tutorialspoint

Example: Appending a char after Casting as CodePoint to StringBuilder

We can also pass a character value as a parameter to this method by typecasting it, and the method will still return the appended sequence.


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

      StringBuilder word = new StringBuilder("Tutorialspoint");
      char ch = '!';

      System.out.println("The input sequence: " + word);
      System.out.println("The character to be appended: " + ch);
      System.out.println("The appended sequence: " + word.appendCodePoint((int)ch));
   }
}

Output

Let us compile and run the program above, to produce the output as displayed below −

The input sequence: Tutorialspoint
The character to be appended: !
The appended sequence: Tutorialspoint!

Example: Facing Compilation Error while using Null

But if we pass a null value as an argument to this method, the method will throw a compile-time error.


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

      StringBuilder word = new StringBuilder("Tutorialspoint");
      int ch = null;

      // a compile time error as there is no code point for null values
      System.out.println("The appended sequence: " + word.appendCodePoint(ch));
   }
}

Compile Time Error

If we try to compile and run the program above, a compile time error is raised as given below −

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
        Type mismatch: cannot convert from null to char
        at StringBuilderAppend_cdp.main(StringBuilderAppend_cdp.java:7)

Example: Facing Exception while using invalid CodePoint

Suppose we pass a code point that does not exist in the Unicode system as an argument to the method, the method will throw an IllegalArgumentException.


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

      StringBuilder word = new StringBuilder("Tutorialspoint");
      int cp = -123;

      // throws an exception
      System.out.println("The appended sequence: " + word.appendCodePoint(cp));
   }
}

Exception

If we try to compile and run the program above, the IllegalArgumentException is thrown as codepoints are not negative −

Exception in thread "main" java.lang.IllegalArgumentException: Not a valid Unicode code point: 0xFFFFFF85
        at java.base/java.lang.Character.toChars(Character.java:9493)
        at java.base/java.lang.AbstractStringBuilder.appendCodePoint(AbstractStringBuilder.java:962)
        at java.base/java.lang.StringBuilder.appendCodePoint(StringBuilder.java:443)
        at StringBuilderAppend_cdp.main(StringBuilderAppend_cdp.java:10)
java_lang_stringbuilder.htm
Advertisements