Topic outline

    • In each programming language, characters are considered one of the basic data types. Basic text-based computations are also performed for Java using the 'char' data type, which is the primary data type for Java.


      Character Wrapper Class 


      But often you may come across situations in development where, instead of simple data type (char), you might need to implement objects. Java programmers may use the wrapper class, which has a 'Character' class replacement of the char data sort, to achieve this facility. This class includes a wide range of practical classes and methods that are useful for dealing with characters in complicated programmes, making it easier for programmers to control characters.

      Programmers have to implement Character object of the wrapper class using the Character constructor, something like this:


      Character letterFour = new Character('d');


      Predefined methods of character class- 



      Escape Sequence :


      An escape sequence is a character followed by a backslash (\) and the compiler has a special sense. 


      Following table shows the Java escape sequences −



      Escape Sequence Description :


      \t Inserts a tab in the text at this point.

      \b Inserts a backspace in the text at this point.

      \n Inserts a newline in the text at this point.

      \r Inserts a carriage return in the text at this point.

      \f Inserts a form feed in the text at this point.

      \' Inserts a single quote character in the text at this point.

      \" Inserts a double quote character in the text at this point.

      \\ Inserts a backslash character in the text at this point.


      Character Methods :


      Following is the list of the important instance methods that all the subclasses of the Character class implement −


      1. isLetter()

      Determines whether the specified char value is a letter.


      2. isDigit()

      Determines whether the specified char value is a digit.


      3. isWhitespace()

      Determines whether the specified char value is white space.


      4. isUpperCase()

      Determines whether the specified char value is uppercase.


      5. isLowerCase()

      Determines whether the specified char value is lowercase.


      6. toUpperCase()

      Returns the uppercase form of the specified char value.


      7. toLowerCase()

      Returns the lowercase form of the specified char value.


      8. toString()

      Returns a String object representing the specified character value that is, a one-character string.



      — The following is an example of character in Java -



      package examples;


      public class characters_exmaple {

      public static void main(String[] args)

      {

      //character object

      Character character = new Character('A');

      System.out.println(character);

      //various operations on character

      System.out.println(Character.isLetter('B'));

      System.out.println(Character.isDigit('B'));

      System.out.println(Character.isDigit('9'));

      System.out.println(Character.toLowerCase('B'));

      }


      }


      Output- 


      The character value - A

      This will be true - true

      This will be false - false

      This will be true - true

      This will be return lowercase - b