java.lang.Character
The Character class simply a wrapper class for the primitive type char. It wraps the char primitive value to an object. An object of type Character contains a single field whose type is char. So what would be the benefit if wraps around the primitive data type to an object? Well basically we would be able to access all the methods that is readily available on the Character class which is very useful. In addition, this class provides several methods for determining a character’s category (lowercase letter, digit, etc.) and for converting characters from uppercase to lowercase and vice versa. We will go over each and every method available to Byte class thoroughly.
Character information is based on the Unicode Standard, version 6.2.0.
The methods and data of class Character are defined by the information in the UnicodeData file that is part of the Unicode Character Database maintained by the Unicode Consortium. This file specifies various properties including name and general category for every defined Unicode code point or character range.
Character Class Syntax
public final class Character
extends Object
implements Serializable, Comparable<Character>
Character Class Compatibility Version
Character Class is available since JDK 1.0
Character Class Basic Usage
The Character class as part of the java.lang package is one of the classes of the java api that is being widely used. Let’s tackle the basics of using the Character class. To begin with, let’s tackle first how to instantiate a Character object
1 |
Character value = new Character('c'); |
Basically the declaration of a Character object is similar with the method on we assign a primitive data type. Similarly, to convert Character primitive the Byte object has only two possible values, which is either true or false. This is a straightforward method to instantiate a Character object type. Let’s take another way to do this.
1 |
Byte value = 12; |
The 2nd method is using the autoboxing concept of java programming language which directly converts a primitive data type to its corresponding wrapper class. If you would like to go more into details, I am recommending for you to read the official java documentation regarding autoboxing and unboxing.
There is only one constructor available to Character class which we can use to instantiate a Character object:
Constructor | Description |
---|---|
Character(char value) | Constructs a newly allocated Character object that represents the specified char value. |
Character Class Test of Equality
Normally when we do equality test for Character primitive data type we do like this:
1 2 3 4 5 6 7 8 |
Character c1= 'c'; char c2 = 'c'; if(c1==c2){ System.out.println("They are equal"); } else{ System.out.println("Values are not equal"); } |
This will no longer work in dealing with Character object type. To test equality we need to make use of equals which is a method inherited from Object class.
1 2 3 4 5 6 7 8 |
Character c1 = 'c'; Character c2 = new Character('c'); if(c1.equals(c2)){ System.out.println("They are equal"); } else{ System.out.println("They are not equal"); } |
Character Class Method Usage Examples
Modifier and Type | Method and Description |
---|---|
static int | charCount(int codePoint) Determines the number of char values needed to represent the specified character (Unicode code point). |
char | charValue() Returns the value of this Character object. |
static int | codePointAt(char[] a, int index) Returns the code point at the given index of the char array. |
static int | codePointAt(char[] a, int index, int limit) Returns the code point at the given index of the char array, where only array elements with index less than limit can be used. |
static int | codePointAt(CharSequence seq, int index) Returns the code point at the given index of the CharSequence. |
static int | codePointBefore(char[] a, int index) Returns the code point preceding the given index of the char array. |
static int | codePointBefore(char[] a, int index, int start) Returns the code point preceding the given index of the char array, where only array elements with index greater than or equal to start can be used. |
static int | codePointBefore(CharSequence seq, int index) Returns the code point preceding the given index of the CharSequence. |
static int | codePointCount(char[] a, int offset, int count) Returns the number of Unicode code points in a subarray of the char array argument. |
static int | codePointCount(CharSequence seq, int beginIndex, int endIndex) Returns the number of Unicode code points in the text range of the specified char sequence. |
static int | compare(char x, char y) Compares two char values numerically. |
int | compareTo(Character anotherCharacter) Compares two Character objects numerically. |
static int | digit(char ch, int radix) Returns the numeric value of the character ch in the specified radix. |
static int | digit(int codePoint, int radix) Returns the numeric value of the specified character (Unicode code point) in the specified radix. |
boolean | equals(Object obj) Compares this object against the specified object. |
static char | forDigit(int digit, int radix) Determines the character representation for a specific digit in the specified radix. |
static byte | getDirectionality(char ch) Returns the Unicode directionality property for the given character. |
static byte | getDirectionality(int codePoint) Returns the Unicode directionality property for the given character (Unicode code point). |
static String | getName(int codePoint) Returns the Unicode name of the specified character codePoint, or null if the code point is unassigned. |
static int | getNumericValue(char ch) Returns the int value that the specified Unicode character represents. |
static int | getNumericValue(int codePoint) Returns the int value that the specified character (Unicode code point) represents. |
static int | getType(char ch) Returns a value indicating a character’s general category. |
static int | getType(int codePoint) Returns a value indicating a character’s general category. |
int | hashCode() Returns a hash code for this Character; equal to the result of invoking charValue(). |
static int | hashCode(char value) Returns a hash code for a char value; compatible with Character.hashCode(). |
static char | highSurrogate(int codePoint) Returns the leading surrogate (a high surrogate code unit) of the surrogate pair representing the specified supplementary character (Unicode code point) in the UTF-16 encoding. |
static boolean | isAlphabetic(int codePoint) Determines if the specified character (Unicode code point) is an alphabet. |
static boolean | isBmpCodePoint(int codePoint) Determines whether the specified character (Unicode code point) is in the Basic Multilingual Plane (BMP). |
static boolean | isDefined(char ch) Determines if a character is defined in Unicode. |
static boolean | isDefined(int codePoint) Determines if a character (Unicode code point) is defined in Unicode. |
static boolean | isDigit(char ch) Determines if the specified character is a digit. |
static boolean | isDigit(int codePoint) Determines if the specified character (Unicode code point) is a digit. |
static boolean | isHighSurrogate(char ch) Determines if the given char value is a Unicode high-surrogate code unit (also known as leading-surrogate code unit). |
static boolean | isIdentifierIgnorable(char ch) Determines if the specified character should be regarded as an ignorable character in a Java identifier or a Unicode identifier. |
static boolean | isIdentifierIgnorable(int codePoint) Determines if the specified character (Unicode code point) should be regarded as an ignorable character in a Java identifier or a Unicode identifier. |
static boolean | isIdeographic(int codePoint) Determines if the specified character (Unicode code point) is a CJKV (Chinese, Japanese, Korean and Vietnamese) ideograph, as defined by the Unicode Standard. |
static boolean |