How to Use the Character Class in Java

The Java programming language comes bundled with a library that provides functions that are useful in general programming. One such class is the Character class. The Character class provides a wrapper to "char" primitive objects, which are used to store a single letter, number or symbol. The wrapper allows you to perform functions on "char" objects, like testing whether or not a "char" is a letter or a number. Learning how to use the basic functions of the Character class can help you develop better Java programs.

Things You'll Need

  • Java Software Development Kit with NetBeans Integrated Development Environment (IDE) Bundle
Show More

Instructions

    • 1

      Load the NetBeans IDE by clicking on its program icon. When the program loads, navigate to "New/New Project" and select "Java Application" from the list on the right-hand side of the screen. A new project is created, and a source code file appears in the NetBeans text editor. The source code file has an empty main function.

    • 2

      Import the Character class by writing the following statement at the top of your source code file:

      import java.lang.Character;

    • 3

      Declare a new Character object named "c" and store the letter a in it. You can do this by writing the following code within the curly brackets of the main function:

      Character c = new Character('a');

    • 4

      Access the value of the "char" primitive data using the charValue() function of the Character class. You can do this by writing the following line beneath the line written in the previous step:

      System.out.println(c.charValue());

    • 5

      Test whether or not the Character data holds a digit or not using the isDigit() function, like this:

      System.out.println(Character.isDigit(c));

    • 6

      Test whether or not the Character data holds a letter or not using the isLetter() function, like this:

      System.out.println(Character.isLetter(c));

    • 7

      Transform the Character object into a string using the toString() function, like this:

      String s = c.toString();

    • 8

      Execute the program by pressing F6. The program will generate the following output:

      a

      false

      true

Related Searches:

References

Resources

Comments

You May Also Like

Related Ads

Featured