
- 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 codePointAt() Method
The Java StringBuffer codePointAt() method is used to get the character (its Unicode code point) present at an index of a StringBuffer. The index ranges from 0 to length() - 1.
Note − If the char value specified at the given index is in the high-surrogate range and its following index is less than the length of this sequence, and the char value at this following index is in the low-surrogate range, then the supplementary code point corresponding to this surrogate pair is returned. Otherwise, the char value at the given index is returned.
Syntax
Following is the syntax for Java StringBuffer codePointAt() method
public int codePointAt(int index)
Parameters
- index − This is the index to the char values.
Return Value
This method returns the code point value of the character at the index.
Example: Getting codePoint at given Index
If we initialize the StringBuffer object with the letters as CharSequence, the method returns the character present in the argument index of the StringBuffer.
The following example shows the usage of Java StringBuffer codePointAt() method.
package com.tutorialspoint; public class StringBufferDemo { public static void main(String[] args) { StringBuffer buff = new StringBuffer("TUTORIALS"); System.out.println("buffer = " + buff); // returns the codepoint at index 5 int retval = buff.codePointAt(5); System.out.println("Character(unicode point) = " + retval); buff = new StringBuffer("amrood admin "); System.out.println("buffer = " + buff); // returns the codepoint at index 6 i.e whitespace character retval = buff.codePointAt(6); System.out.println("Character(unicode point) = " + retval); } }
Output
Let us compile and run the above program, this will produce the following result −
buffer = TUTORIALS Character(unicode point) = 73 buffer = amrood admin Character(unicode point) = 32
Example: Getting codePoint at given Index
In another example, we are creating a StringBuffer object and initializing it with a digit sequence. Then, the method is invoked on this object with an index as its argument.
public class StringBufferCodePointAt { public static void main(String[] args) { StringBuffer obj = new StringBuffer("78343890"); int id = 5; System.out.println("The code point at index " + id + " is: " + obj.codePointAt(id)); } }
Output
Once we compile and run the program above, the output is displayed as follows −
The code point at index 5 is: 56
Example: Getting codePoint at given Index
If we initialize the StringBuffer object with the symbols as CharSequence, and an index is passed as the argument, the method returns the character present at that index.
public class StringBufferCodePointAt { public static void main(String[] args) { StringBuffer obj = new StringBuffer("#$%^&*("); int id = 3; System.out.println("The code point at index " + id + " is: " + obj.codePointAt(id)); } }
Output
The output for the above program is printed as −
The code point at index 3 is: 94
Example: Facing Exception while Getting codePoint at Negative Index
If the index passed as an argument to this method is invalid (either negative or greater than charsequence length), an exception is thrown.
public class StringBufferCodePointAt { public static void main(String[] args) { StringBuffer obj = new StringBuffer("Tutorialspoint"); int id = -2; System.out.println("The code point at index " + id + " is: " + obj.codePointAt(id)); //throws exception } }
Exception
If we try to compile and run the program above, an exception is thrown instead of printing the output −
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: Index -2 out of bounds for length 14 at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:55) at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:52) at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:213) at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:210) at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:98) at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:106) at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:302) at java.base/java.lang.String.checkIndex(String.java:4570) at java.base/java.lang.AbstractStringBuilder.codePointAt(AbstractStringBuilder.java:389) at java.base/java.lang.StringBuffer.codePointAt(StringBuffer.java:252) at StringBufferCodePointAt.main(StringBufferCodePointAt.java:11)