Character and Boolean Wrapper classes in Java
|Wrapper classes play an important part in creating objects out of primitive data types. In this tutorial, we shall see how char and boolean type data are wrapped into objects using the wrapper classes- Character and Boolean respectively and its various methods.
Character Wrapper Class
Character class wraps the primitive type char data into objects. The constructor for Character class is shown below:
Character(char ch)
ch contains the value which will be wrapped into the object. The declaration syntax is shown below:
Character ch = new Character(A);
Read More – Byte and Short Wrapper Class
There are a few constants defined by the Character class. These are given below:
CONSTANTS | DESCRIPTION |
---|---|
BYTES | The width of a char in bytes. It was added by JDK8 |
MAX_RADIX | The largest Radix |
MIN_RADIX | The smallest Radix |
MAX_VALUE | The largest character value |
MIN_VALUE | The smallest character value |
TYPE | The Class object for char. |
Methods defined by Character Wrapper class
METHODS | DESCRIPTION |
---|---|
static char forDigit(int num, int radix) | Returns the digit character associated with num using the specified radix |
static int digit(char digit, int radix) | Returns the digit associated with the character digit using the specified radix. |
int compareTo(character c) | Compares the invoking object with c and returns 0 if equal. Returns a negative value if invoking object has a lower value and returns a positive value otherwise. |
static boolean isDefined(char ch) | Returns true if ch is defined in Unicode, otherwise false. |
static boolean isDigit(char ch) | Returns true if ch is a digit, otherwise false. |
static boolean isIdentifierIgnorable(char ch) | Returns true if ch should be ignored as an identifier, otherwise false. |
static boolean isISOControl(char ch) | Returns true if ch is ISO control character, otherwise false. |
static boolean isJavaIdentifierStart(char ch) | Returns true if ch is allowed as the first character of a Java identifier, otherwise false. |
static boolean isJavaIdentifierPart(char ch) | Returns true if ch is allowed as a part of a Java identifier(except first), otherwise false. |
static boolean isLetter(char ch) | Returns true if ch is a letter, otherwise false. |
static boolean isLetterOrDigit(char ch) | Returns true if ch is a letter or digit, otherwise false. |
static boolean isLowerCase(char ch) | Returns true if ch is a lowercase letter, otherwise false. |
static boolean isUpperCase(char ch) | Returns true if ch is a uppercase letter, otherwise false. |
static boolean isMirrored(char ch) | Returns true if ch is a mirrored Unicode character, otherwise false(Mirrored code is one that is reversed for text displayed right-to-left ). |
static boolean isSpaceChar(char ch) | Returns true if ch contains a Unicode space, otherwise false. |
static boolean isTitleCase(char ch) | Returns true if ch is a Unicode titlecase character, otherwise false. |
static boolean isUnicodeIdentifierStart(char ch) | Returns true if ch is allowed as the first character of a Unicode identifier, otherwise false. |
static boolean isJavaIdentifierPart(char ch) | Returns true if ch is allowed as a part of a Unicode identifier(except first), otherwise false. |
static boolean isWhitespace(char ch) | Returns true if ch is a Whitespace, otherwise false. |
static char toLowerCase(char ch) | Returns the character after converting it into lowercase. |
static char toUpperCase(char ch) | Returns the character after converting it into uppercase. |
static char toTitleCase(char ch) | Returns the character after converting it into titlecase. |
Unicode Code Point for Character
JDK5 introduced a 32-bit pattern for the Character class to support the Unicode characters. Initially, it was the same size as char, i.e. 16-bit range from 0 to FFFF. Now, it has expanded to a range from 0 to 10FFFF. Now, to better understand the divisions here are few terms related to it:
- Code point: Includes character in the range 0 to 10FFFF.
- Supplemental Characters: Includes character belonging in the range greater than FFFF.
- Basic multilingual plane(BMP): Includes character from 0 to FFFF.
This expansion of Character due to Unicode posed a difficulty for the supplementary characters because the size of char is 16-bit. To resolve this problem there are two alternatives:
First, Java used two characters to represent the supplemental character. The first one is known as the high surrogate and the second one is the low surrogate.
Secondly, Java used int to overload the pre-existing methods in Character because int is large enough to hold any character.
The new methods and the overridden methods to accommodate code point are shown below:
METHODS | DESCRIPTION |
---|---|
static int charCount(int cp) | Returns 1 if cp can be represented as a single character. Returns 2 if two characters are needed. |
static int codePointAt(CharSequence chars, int loc) | Returns code point at location loc. |
static int codePointAt(char chars[], int loc) | Returns code point at location loc. |
static int codePointBefore(CharSequence chars, int loc) | Returns code point at location preceding loc. |
static int codePointBefore(char chars[], int loc) | Returns code point at location preceding loc. |
static boolean isBmpCodePoint(int cp) | Returns true if cp is a part of BMP, otherwise false. |
static boolean isHighSurrogate(char ch) | Returns true if cp contains a valid high surrogate character, otherwise false. |
static boolean isLowSurrogate(char ch) | Returns true if cp contains a valid low surrogate character, otherwise false. |
static boolean isSupplementaryCodePoint(int cp) | Returns true if cp contains a supplemental character, otherwise false. |
static boolean isSurrogatePair(char highch, char lowch) | Returns true if highch and lowch form a valid surrogate pair, otherwise false. |
static boolean isValidCodePoint(int cp) | Returns true if cp contains a valid code point, otherwise false. |
static char[] toChars(int cp) | Converts code point in cp to its char equivalent, which might require two chars. So, an array is returned. |
static int toChars(int cp, char target[], int loc) | Converts code point in cp to its char equivalent and stores it in target beginning at loc. Returns 1 if cp can be represented by a single char otherwise, returns 2. |
static int toCodePoint(char highch, char lowch) | Converts highch and lowch into their equivalent code point. |
Boolean
Boolean is a thin type wrapper which is basically used to wrap the values true and false. This is used when the programmer needs to pass the value by reference. Boolean has the following constructors:
Boolean(boolean boolValue) Boolean(String boolStr)
boolValue can contain either true or false and the boolStr can contain the string “true” or “false“(either uppercase or lowercase). The Boolean class defines the constants TRUE and FALSE which define the boolean true and false respectively. It also defines TYPE which is the class object for boolean.
Methods defined by Boolean Wrapper Class
METHODS | DESCRIPTION |
---|---|
boolean booleanValue() | Returns boolean equivalent. |
static int compare(boolean b1, boolean b2) | Returns 0 if b1 and b2 contain equal value. Returns positive if b1 is true and b2 is false and returns a negative value otherwise. |
int compareTo(Boolean b) | Returns 0 if invoking object and b contain equal values. Returns a positive value if invoking object is true and b is false and returns negative value otherwise. |
boolean equals(Object boolObj) | Returns true if invoking object and boolObj contain same value, otherwise false. |
static boolean getBoolean(String propertyName) | Returns true if system property specified by propertyName is true, otherwise false. |
int hashCode() | Returns the hash code for the invoking object. |
static int hashCode(boolean boolVal) | Returns the hash code for boolVal. It was added by JDK8. |
static boolean logicalAnd(boolean b1, boolean b2) | Returns the result after performing a logical AND of b1 and b2. It was added by JDK8. |
static boolean logicalOr(boolean b1, boolean b2) | Returns the result after performing a logical OR of b1 and b2. It was added by JDK8. |
static boolean logicalXor(boolean b1, boolean b2) | Returns the result after performing a logical XOR of b1 and b2. It was added by JDK8. |
static boolean parseBoolean(String str) | Returns true if str contains “true“(any case), otherwise false. |
String toString() | Returns String equivalent of the invoking object. |
static String toString(boolean boolVal) | Returns String equivalent of boolVal. |
static Boolean valueOf(boolean boolVal) | Returns Boolean equivalent of boolVal. |
static Boolean valueOf(String boolStr) | Returns true if boolStr contains “true“(any case), otherwise false. |
Program to show the use of Character and Boolean Wrapper class
Unable to retrieve the Code part. Please reload again. Notify us if the problem still persists. Till we work on this you can view code on URL below. Please visit - https://github.com/HiteshGarg/codingeek/blob/master//Java/WrapperClass/CharBoolDemo.java
Output:-
A is digit : false , isLetter : true
ch1 is Whitespace : true
The logical AND of true and false is false
Learning never exhausts the mind.So, do come back for more. Hope this helps and you like the tutorial. Do ask for any queries in the comment box and provide your valuable feedback. Share and subscribe.
Keep Coding!! Happy Coding!!