Java Characters

Characters are considered among the essential data type in every programming language. For Java also, basic text-based computations are done using 'char' data type which is the primary data type for Java.

Example:
char[] chArr ={ 'k', 'a', 'r', 'l', 'o', 's' };

Character Wrapper Class

But at times you may come across situations in development where you might require implementing objects in place of fundamental data type (char). For achieving this facility, Java programmers can make use of wrapper class which has a 'Character' class replacement of char data type. This class provides a wide variety of functional class and methods that come in handy for dealing with characters in complex programs, making the manipulation of characters easier for programmers. Programmers have to implement Character object of the wrapper class using the Character constructor, something like this:

Character letterFour = new Character('d');

Or

Character num = new Character('2');

'new' is a keyword of Java used here for creating a Java object which internally tells the compiler to allocate memory on a heap.

More on Character Wrapper Class

Character letterFour = new Character('d');

Or

Character num = new Character('2');

In the above example, Java compiler internally will create Character object for the programmers. In cases when you will be passing the primary data type (char) value to any user-defined method which is ready to accept the argument as an object, the compiler will be going to automatically convert your 'char' value passed to a Character value (which will be an object). This conversion mechanism is called autoboxing & unboxing when the conversion is done the reverse way.

Example:
Character letter = 'g';
char chh = test('k');

Predefined Methods of Character Class

  • isLetter(): It checks whether the particular character value is a letter or not
  • isDigit(): It checks whether the specific character value is a digit or not.
  • isWhitespace(): It checks whether the particular character value is a white-space or not. White-space comes with three things:
    1. spaces
    2. tab and
    3. new-line
  • isUpperCase(): It checks whether the specific character value is in uppercase or not.
  • isLowerCase(): It checks whether the particular character value is lowercase or not.
  • toUpperCase(): This method is used to convert to the upper-case letter by returning the uppercase form of the specified char value.
  • toLowerCase(): This method is used to convert the specific letter to lower-case letter by returning the lowercase form of the specified char value.
  • toString(): It used to returns a String object representing the specified character value.

Program showing the example of each of them:

public class CharacterClassExample {
    
 public static void main(String argu[]) {
  System.out.println(Character.isLetter('g')); // true
  System.out.println(Character.isLetter('6')); // false
  System.out.println(Character.isDigit('8')); // true
  System.out.println(Character.isWhitespace('\t')); // true
  System.out.println(Character.isUpperCase('k')); // false
  System.out.println(Character.isLowerCase('G')); // false
  System.out.println(Character.toUpperCase('d')); // C
  System.out.println(Character.toLowerCase('K')); // c
  System.out.println(Character.toString('S')); // C
 }
 
}

Scroll Back to Top