- Java.lang Package classes
- 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 extras
- Java.lang - Interfaces
- Java.lang - Errors
- Java.lang - Exceptions
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java - StringBuilder appendCodePoint() Method
The Java StringBuilder appendCodePoint() method is used to append the string representation of a CodePoint value to the current object. This method accepts an integer value representing a codepoint as a parameter and adds the respective character to the current StringBuilder object. After this, the length of the object is increased by a character.
The code point value (Unicode code point) is similar to the ASCII value. it is a string consists of a group of characters and each character is associated with a Unicode value (code point). The appendCodePoint() method throws an exception when we pass the negative code point value to the method.
Syntax
Following is the syntax of the Java StringBuilder appendCodePoint() method −
public StringBuilder appendCodePoint(int codePoint)
Parameters
codePoint − This is the Unicode code point.
Return Value
This method returns the appended code point char values.
Example
If we pass the positive and valid code point value to the method, the appendCodePoint() method, returns the character value of the given code point value.
In the following program, we create a StringBuilder with the value “programming”. We are passing 74 as the code point value to theappendCodePont() method, it appends the “J” (J = 74) to the StringBuilder object.
package com.tutorialspoint; import java.lang.*; public class StringBuilderDemo { public static void main(String[] args) { StringBuilder str = new StringBuilder("programming "); System.out.println("string = " + str); // appends the CodePoint as string to the StringBuilder str.appendCodePoint(74); // print the StringBuilder after appendingCodePoint 74 System.out.println("After appendCodePoint = " + str); } }
Output
On executing the above program, it will produce the following result −
string = programming After appendCodePoint = programming J
Example
If we pass the negative code point value to the method, this method throws an IllegalArgumentException.
In the following program, we are instantiating the StringBuilder class with the value “Hello”. Using the appendCodePoint() method, we are trying to append the Unicode value-90 to the current object.
package com.tutorialspoint.StringBuilder; public class Demo { public static void main(String[] args) { try { //create StringBuilder StringBuilder sb = new StringBuilder("Hello "); System.out.println("The string: " + sb); //declare int variable int codepoint = -90; System.out.println("The given code point value is: " + codepoint); //using appendCodePoint() method System.out.println("After append the code point value: " + sb.appendCodePoint(codepoint)); } catch(Exception e) { e.printStackTrace(); System.out.println("Exception: " + e); } } }
Output
Following is the output of the above program −
The string: Hello The given code point value is: -90 java.lang.IllegalArgumentException: Not a valid Unicode code point: 0xFFFFFFA6 at java.base/java.lang.Character.toChars(Character.java:9218) at java.base/java.lang.AbstractStringBuilder.appendCodePoint(AbstractStringBuilder.java:949) at java.base/java.lang.StringBuilder.appendCodePoint(StringBuilder.java:280) at com.tutorialspoint.StringBuilder.Demo.main(Demo.java:12) Exception: java.lang.IllegalArgumentException: Not a valid Unicode code point: 0xFFFFFFA6
Example
If we pass the zero(0) as the code point value to the method, this method appends the given code point char value to the StringBuilder object.
In this example, we are passing the zero as the code point value to the method, and when the method appendCodePoint() will be called, it appends the zero code point char value to the current object.
package com.tutorialspoint.StringBuilder; public class Demo { public static void main(String[] args) { //create StringBuilder StringBuilder sb = new StringBuilder("Code point "); System.out.println("The string: " + sb); //declare int variable int codepoint = 0; System.out.println("The given code point value is: " + codepoint); //using appendCodePoint() method System.out.println("After appending the code point value: " + sb.appendCodePoint(codepoint)); } }
Output
The above program, produces the following results −
The string: Code point The given code point value is: 0 After appending the code point value: Code point _
Example
If we store the code point values in an array, and when we invoke the method on it, it appends the code point char values one by one to the StringBuilder object.
In the following program, we are creating an object of the StringBuilder class with the value “code Point”. Then, we are, creating an array that stores the code point values {65, 66, 67}. Using the appendCodePoint() method, we are trying to append the code point of the characters into the current object.
package com.tutorialspoint.StringBuilder; public class Demo { public static void main(String[] args) { //create StringBuilder StringBuilder sb = new StringBuilder("Code point "); System.out.println("The string: " + sb); //declare an int[] array int codepoint[] = {65, 66, 67}; System.out.print("The code point values are: "); for(int i = 0; i<codepoint.length; i++) { System.out.print(codepoint[i] + " "); } System.out.println("\nAfter append code point value: "); //using appendCodePoint() method for(int i = 0; i<codepoint.length; i++) { System.out.println(sb.appendCodePoint(codepoint[i]) + " "); } } }
Output
After executing the above program, it will generate the following results −
The string: Code point The code point values are: 65 66 67 After append code point value: Code point A Code point AB Code point ABC