java.lang.Character codePointBefore(char[] a, int index)

Description

The Character.codePointBefore(char[] a, int index) java method returns the code point preceding the given index of the char array. If the char value at (index – 1) in the char array is in the low-surrogate range, (index – 2) is not negative, and the char value at (index – 2) in the char array is in the high-surrogate range, then the supplementary code point corresponding to this surrogate pair is returned. Otherwise, the char value at (index – 1) is returned.

Make a note that the codePointBefore method of Character class is static thus it should be accessed statically which means the we would be calling this method in this format:

Character.codePointBefore(method args)

Non static method is usually called by just declaring method_name(argument) however in this case since the method is static, it should be called by appending the class name as suffix. We will be encountering a compilation problem if we call the java codePointBefore() method non statically.

Notes:

The codePointBefore(char[] a, int index) method throws the following exception:

  • NullPointerException – if a is null.
  • IndexOutOfBoundsException – if the index argument is less than 1 or greater than the length of the char array

Method Syntax

public static int codePointBefore(char[] a, int index)

Method Argument

Data Type Parameter Description
char[] a the char array
int index the index following the code point that should be returned

Method Returns

The codePointBefore(char[] a, int index) method of Character class returns the Unicode code point value before the given index.

Compatibility

Requires Java 1.5 and up

Java Character codePointBefore(char[] a, int index) Example

Below is a simple java example on the usage of codePointBefore(char[] a, int index) method of Character class.

package com.javatutorialhq.java.examples;

import java.util.Scanner;

/*
 * This example source code demonstrates the use of 
 * codePointBefore(CharSequence seq, int index)
 * method of Character class.
 */

public class CharacterCodePointBeforeExample {

	public static void main(String[] args) {

		// initialize a new CharSequence object
		CharSequence cs = "This is a test string";
		
		// Ask for user input
		System.out.print("Enter desired index:");
		
		// use scanner to get the user input
		Scanner s = new Scanner(System.in);
		int index = s.nextInt();
		
		// close the scanner object
		s.close();
		
		// get the code point preceding  the given 
		// index of the CharSequence
		
		int result = Character.codePointBefore(cs, index);
		System.out.println("Result:"+result);
	}

}

Sample Output

Below is the sample output when you run the above example.

java Character codePointBefore(char[] a, int index) example output