Java StringBuffer appendCodePoint() Method



The Java StringBuffer 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 StringBuffer appendCodePoint() method

public StringBuffer appendCodePoint(int codePoint)

Parameters

  • codePoint − a Unicode code point

Return Value

The method returns the reference to this StringBuffer object.

Example

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.

import java.lang.*;

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

      StringBuffer word = new StringBuffer("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

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.

import java.lang.*;

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

      StringBuffer word = new StringBuffer("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

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

import java.lang.*;

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

      StringBuffer word = new StringBuffer("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 StringBufferAppend_cdp.main(StringBufferAppend_cdp.java:7)

Example

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.

import java.lang.*;

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

      StringBuffer word = new StringBuffer("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.StringBuffer.appendCodePoint(StringBuffer.java:443)
        at StringBufferAppend_cdp.main(StringBufferAppend_cdp.java:10)
java_lang_stringbuffer.htm
Advertisements