Java StringBuilder.appendCodePoint() – Examples

In this tutorial, we will learn about the Java StringBuilder.appendCodePoint() function, and learn how to use this function to append a Unicode code point to the StringBuilder, with the help of examples.

appendCodePoint(int codePoint)

StringBuilder.appendCodePoint() appends the string representation of the codePoint argument to the sequence in StringBuilder object.

ADVERTISEMENT

Syntax

The syntax of appendCodePoint() function is

appendCodePoint(int codePoint)

where

ParameterDescription
codePointa Unicode code point

Returns

The function returns a reference to this StringBuilder object.

Example 1 – appendCodePoint(int codePoint)

In this example, we will create a StringBuilder with some initial string literal, and an integer variable holding a Unicode code point. We will then use appendCodePoint() method to append the Unicode code point to the StringBuilder.

Java Program

public class Example { 
    public static void main(String[] args) { 
        StringBuilder stringBuilder = new  StringBuilder("abc"); 
        System.out.println("Original value   : " + stringBuilder); 
  
        // append CodePoint
        int codePoint = 65; 
        stringBuilder.appendCodePoint(codePoint); 
        System.out.println("After appending  : " + stringBuilder); 
    } 
}

Output

Original value   : abc
After appending  : abcA

Example 2 – appendCodePoint(int codePoint)

In this example, we will use appendCodePoint() method to append all the Unicode code points, present in an array, to the StringBuilder.

Java Program

public class Example { 
    public static void main(String[] args) { 
        StringBuilder stringBuilder = new  StringBuilder("abc"); 
        System.out.println("Original value   : " + stringBuilder); 
  
        // CodePoint array
        int[] codePoints = {65, 66, 67, 68};
        
        for(int codePoint: codePoints) {
        	stringBuilder.appendCodePoint(codePoint);
        }
        System.out.println("After appending  : " + stringBuilder); 
    } 
}

Output

Original value   : abc
After appending  : abcABCD

Example 3 – appendCodePoint(int codePoint)

In this example, we will try appending an invalid Unicode code point value to the StringBuilder. appendCodePoint() method thrwos IllegalArgumentException.

Java Program

public class Example { 
    public static void main(String[] args) { 
        StringBuilder stringBuilder = new  StringBuilder("abc"); 
        System.out.println("Original value   : " + stringBuilder); 
  
        // append CodePoint
        int codePoint = 65996665; 
        stringBuilder.appendCodePoint(codePoint); 
        System.out.println("After appending  : " + stringBuilder); 
    } 
}

Output

Original value   : abc
Exception in thread "main" java.lang.IllegalArgumentException
	at java.base/java.lang.Character.toChars(Unknown Source)
	at java.base/java.lang.AbstractStringBuilder.appendCodePoint(Unknown Source)
	at java.base/java.lang.StringBuilder.appendCodePoint(Unknown Source)
	at Example.main(Example.java:8)

Conclusion

In this Java Tutorial, we have learnt the syntax of Java StringBuilder.appendCodePoint() function, and also learnt how to use this function with the help of examples.