articles

Home / DeveloperSection / Articles / Wrapper Classes: A Few Additions in J2SE 5.0 (Part-2)

Wrapper Classes: A Few Additions in J2SE 5.0 (Part-2)

zack mathews 1670 28-May-2016
Character Class

The Character class that wraps a char data type provides several utility methods to operate on character data. We can test whether the given character is a digit, a letter, a lowercase character, and so on. The simple program given here illustrates the use of some of these methods.

Program Code
public class CharWrapper {

                   public static void main(String args[]) throws Exception {
                     int digitCount = 0, letterCount = 0, lcCount = 0, ucCount = 0, wsCount = 0;
                             for (int i = 0; i < 0xFF; i++) {
                                      if (Character.isDigit(i)) {
                                                digitCount++;
                                      }
                                      if (Character.isLetter(i)) {
                                                letterCount++;
                                      }
                                      if (Character.isLowerCase(i)) {
                                                lcCount++;
                                      }
                                      if (Character.isUpperCase(i)) {
                                                ucCount++;
                                      }
                                      if (Character.isWhitespace(i)) {
                                                wsCount++;
                                      }
                             }
                             System.out.println("No of digits: " + digitCount);
                             System.out.println("No of letters: " + letterCount);
                             System.out.println("No of lower case letters: " + lcCount);                              System.out.println("No of upper case letters: " + ucCount);                              System.out.println("No of white space characters: " + wsCount);
                     }

}
 
The program tests each character in the numeric range 0 to 255 for

·         a digit,

·         a letter,

·         a lowercase letter,

·         an uppercase letter, and

·         a white space.

 It counts the occurrence of each of these types and finally prints the result to the user console.

Output
When we run the program, we see the following output:
No of digits: 10
No of letters: 116
No of lower case letters: 60
No of upper case letters: 56
No of white space characters: 10

 Extended Support for Unicode Code Point

Unicode characters that occupy 16 bits have now been extended to 32 bits to accommodate more characters. Thus, now the characters range from 0 to 0x10FFFF. The characters having values greater than 0xFFFF are called supplemental characters. Let’s again runs the program given above, but this time modifying the loop count to 0x10FFFF, as follows:

for (int i = 0; i < 0x10FFFF; i++) {

The program output after this modification is shown here:

No of digits: 268

No of letters: 90547
No of lower case letters: 1415
No of upper case letters: 1190
No of white space characters: 27
No of digits: 420
No of letters: 100520

 The output will vary depending on our OS and the selected character set. The new Character class has overloaded many of its existing methods that operate on the char type (which is a 16-bit number) to use an int type (which is 32 bits wide). Besides these, the Character class has also introduced several new methods that allow us to work on the new Unicode character set.

The Void Wrapper
Lastly, let’s discuss the wrapper on the void data type.

·         The Void class has one field called Type.

·         This field holds a reference to the Class object that represents the void type.

·         We cannot instantiate the Void class.

·         We can simply print its class type as follows:

System.out.println("The Class for Void is " + Void.TYPE);

This prints the following message to the terminal:

The Class for Void is void


Updated 18-Dec-2017

Leave Comment

Comments

Liked By