The Character Class

The Character class defines a myriad of constants, including the following, which represent the minimum and the maximum values of the char type (§2.2, p. 42):

Click here to view code image

Character.MIN_VALUE
Character.MAX_VALUE

The Character class also defines a plethora of static methods for handling various attributes of a character, and case issues relating to characters, as defined by the Unicode standard:

Click here to view code image

static int     getNumericValue(char ch)
static boolean isLowerCase(char ch)
static boolean isUpperCase(char ch)
static boolean isTitleCase(char ch)
static boolean isDigit(char ch)
static boolean isLetter(char ch)
static boolean isLetterOrDigit(char ch)
static char    toUpperCase(char ch)
static char    toLowerCase(char ch)
static char    toTitleCase(char ch)

The following code converts a lowercase character to an uppercase character:

Click here to view code image

char ch = ‘a’;
if (Character.isLowerCase(ch)) ch = Character.toUpperCase(ch); // A

The Boolean Class

In addition to the common utility methods for wrapper classes discussed earlier in this section, the Boolean class defines the following wrapper objects to represent the primitive values true and false, respectively:

Boolean.TRUE
Boolean.FALSE

Converting Strings to Boolean Values

The wrapper class Boolean defines the following static method, which returns the boolean value true only if the String argument is equal to the string “true”, ignoring the case; otherwise, it returns the boolean value false. Note that this method does not throw any exceptions, as its numeric counterparts do.

Click here to view code image

static boolean parseBoolean(String str)

Click here to view code image

boolean b1 = Boolean.parseBoolean(“TRUE”);      // true.
boolean b2 = Boolean.parseBoolean(“true”);      // true.
boolean b3 = Boolean.parseBoolean(“false”);     // false.
boolean b4 = Boolean.parseBoolean(“FALSE”);     // false.
boolean b5 = Boolean.parseBoolean(“not true”);  // false.
boolean b6 = Boolean.parseBoolean(“null”);      // false.
boolean b7 = Boolean.parseBoolean(null);        // false.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *