![java package tutorial](/java/lang/images/java.jpg)
- 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 - Character codePointBefore() Method
Description
The Java Character codePointBefore() will retrieve the code point of a character preceding an index of a certain char array. These code points can represent ordinary character and supplementary characters as well.
Supplementary code point characters will be obtained when the value preceding the given index in a char array satisfies the given conditions below −
If the char value at (index-2) belongs to the high-surrogate range (U+D800 to U+DBFF) and not negative.
If the value at the preceding index belongs to the low surrogate range (U+DC00 to U+DFFF).
Ordinary code point characters will be obtained in any other case.
Note − This method occurs in several polymorphic forms with different parameters.
Syntax
Following is the various syntaxes for Java Character codePointBefore() method using different and extended parameters.
public static int codePointBefore(char[] a, int index) (or) public static int codePointBefore(CharSequence seq, int index) (or) public static int codePointBefore(char[] a, int index, int start)
Parameters
a − The char array
index − The index following the code point that should be returned
start − The index of the first array element in the char array.
seq − The CharSequence instance
Return Value
This method returns the Unicode code point value before the given index.
Getting Code Point of a Preceeding Character in an Array of Char at given Index Example
The following example shows the usage of the Java Character codePointBefore() method. When we take a simple character array and a specified index less than the array length as the arguments for the method, the return value will be the code point of the preceeding character present in that index.
package com.tutorialspoint; public class CharacterDemo { public static void main(String[] args) { // create a char array c and assign values char[] c = new char[] { 'a', 'b', 'c', 'd', 'e' }; // create and assign value to index int index = 2; // create an int res int res; // assign result of codePointBefore on array c at index to res res = Character.codePointBefore(c, index); String str = "Unicode code point is " + res; // print res value System.out.println( str ); } }
Output
Let us compile and run the above program, this will produce the following result −
Unicode code point is 98
Getting Code Point of a Preceeding Character in a CharSequence at given Index Example
When we pass a CharSequence and an index value as the arguments to the codePointBefore(CharSequence seq, int index) method, the return value is the preceeding character present at the index in the given CharSequence.
package com.tutorialspoint; public class CharacterDemo { public static void main(String[] args) { CharSequence seq = "Hello"; int index = 4; int res; // assign result of codePointBefore on seq at index to res res = Character.codePointBefore(seq, index); String str = "Unicode code point is " + res; System.out.println( str ); } }
Output
Let us compile and run the above program, this will produce the following result −
Unicode code point is 108
Example
The following example shows the usage of Java Character codePointBefore(char[] a, int index, int start) method.
package com.tutorialspoint; public class CharacterDemo { public static void main(String[] args) { char[] c = new char[] { 'A', 'b', 'C', 'd'}; int index = 3, start = 1; int res; // assign result of codePointBefore on c array at index to res using start res = Character.codePointBefore(c, index, start); String str = "Unicode code point is " + res; System.out.println( str ); } }
Output
Let us compile and run the above program, this will produce the following result −
Unicode code point is 67
Facing Exception while Getting Code Point of a Preceeding Character in a char array at invalid Index Example
if an invalid index is passed as an argument to the method, a ArrayIndexOutOfBoundsException Exception is raised.
package com.tutorialspoint; public class CharacterDemo { public static void main(String[] args) { // create a char array c and assign values char[] c = new char[] { 'a', 'b', 'c', 'd', 'e' }; // create and assign a negative value to index to throw an ArrayIndexOutOfBoundsException exception int index = -1; // create an int res int res; // assign result of codePointBefore on array c at index to res res = Character.codePointBefore(c, index); String str = "Unicode code point is " + res; // print res value System.out.println( str ); } }
Exception
On compiling and executing the program above, the method throws an exception as follows −
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -2 at java.lang.Character.codePointBeforeImpl(Character.java:5055) at java.lang.Character.codePointBefore(Character.java:5016) at com.tutorialspoint.CharacterDemo.main(CharacterDemo.java:19)
Facing Exception while Getting Code Point of a Preceeding Character in a null CharSequence at given Index Example
But if the CharSequence is initialized with a null value and passed as an argument to the method, a NullPointer Exception is raised.
package com.tutorialspoint; public class CharacterDemo { public static void main(String[] args) { CharSequence seq = null; int index = 9; int output = Character.codePointBefore(seq, index); System.out.println(output); } }
Exception
On compiling and executing the program above, the method throws an exception as follows −
Exception in thread "main" java.lang.NullPointerException at java.lang.Character.codePointBefore(Character.java:4984)at com.tutorialspoint.CharacterDemo.main(CharacterDemo.java:10)