GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialspoint.com — Java StringBuffer appendCodePoint() Method
Selected Reading
Java StringBuffer appendCodePoint() MethodThe 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. SyntaxFollowing is the syntax for the Java StringBuffer appendCodePoint() method public StringBuffer appendCodePoint(int codePoint) Parameters
Return ValueThe method returns the reference to this StringBuffer object. Example: Appending String representation of CodePoint to StringBufferIn 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));
}
}
OutputOn 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 StringBufferWe 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));
}
}
OutputLet 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 NullBut 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 ErrorIf 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 CodePointSuppose 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));
}
}
ExceptionIf 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
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |