
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
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: Appending String representation of CodePoint to StringBuffer
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 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: Appending a char after Casting as CodePoint to StringBuffer
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 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: 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 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: 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 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)