You probably know the difference between primitive types and wrapper classes. I recommend a brief review of this under Java Data Types. You also know about boxing and unboxing as well.
Furthermore we have considered The Numbers Class. This time we would look at the Characters class.
1. Introduction to the Character Class
A character is a single character enclosed in single quote. For instance, the code below declares and assigns two characters.
char char1 = 'A'; char char2 = 'B'; //unicode character for & char uniChar = '\u0026'; //a character array char charArray[] = {'J', 'A', 'V', 'A'};
Listing 1.0: char primitive types
All the characters we have declared in Listing 1.0 are primitive type. However, we may need to use the corresponding object type. So the equivalent wrapper class for this is the Character class.
You can create a new character object by calling the constructor of this class. This you use with the new keyword. An example is given below.
Character ch1 = new Character('J');
The code creates a new character object, ‘J’. You recall that this is example of boxing. That is, we take a primitive type ‘J’ and box it into a Character object.
Furthermore, we have various useful methods the Character class provides to help us manage Character objects. Find the list in Table 1.0
2. Escape Sequences
We also use Characters are used in escape sequences. Now, escape sequence is a combination of a backslash (\) and a character. Hence, you may need it at certain times.
For example, you use the escape sequence \n represents a new line. You probably have used this to add a new line to the end of a string.
You can find a list of escape in Table 1.0.
| Escape Sequence | Brief Description |
|---|---|
| \n | Adds a newline in the text at the point it appears. |
| \b | Ads a backspace in the text at the point it appears. |
| \t | Adds a tab in the text at the point it appears. |
| \r | Adds a carriage return in the text at the point it appears. |
| \f | Adds a form feed in the text at this the it appears. |
| \’ | Adds a single quote character in the text at this the it appears. |
| \” | Ads a double quote character in the text at the point it appears. |
| \\ | Adds a backslash character in the text at this the it appears. |
Table 1.0: List of Escape sequences
So let’s write a program that makes use of some of the escape sequence. I therefore recommend you try it. Also try out others not in the program. See Listing 1.1.
public class Tester { public static void main(String args[]) { //notice the tab System.out.println("Java\tTutorials"); //Notice the quotes System.out.println("Your are learning \"Java\" "); //Notice the carriage return(same as enter key) System.out.println("Good\rJob"); } }
Listing 1.1: Use of Escape Sequences
If you run the code in Listing 1.1, you will have the following output:
Java Tutorials
Your are learning "Java"
Good
Job
3. Character Methods
Similar to the Numbers wrapper class, the Character wrapper class provide us a number of useful methods. I therefore provide a list of these methods in Table 1.1.
| SN | Method and Description |
|---|---|
| 1 | isLetter() Checks if the given char value is a letter. If yes return true, else return false. |
| 2 | isDigit() Checks if the given char value is a digit. If yes return true, else return false |
| 3 | isWhitespace() Checks if the given char value is white space. If yes return true, else return false |
| 4 | isUpperCase() Checks if the given char value is uppercase. If yes return true, else return false |
| 5 | isLowerCase() Checks if the given char value is lowercase. If yes return true, else return false |
| 6 | toUpperCase() Returns the uppercase form of the given char value. |
| 7 | toLowerCase() Returns the lowercase form of the given char value. |
| 8 | toString() Returns a String object representing the given character value that is, a one-character string. |
Table 1.1: Character Methods
I have therefore provided a code to test some of these method. I also recommend you try them yourself.
public class Tester { public static void main(String args[]) { char x = '7'; char a = 'A'; char b = 'b'; //returns true since 7 is a number System.out.println(Character.isDigit(x)); //returns false since 7 not a digit System.out.println(Character.isLetter(x)); //returns true since A is uppercase System.out.println(Character.isUpperCase(a)); //changes b to B System.out.println(Character.toUpperCase(b)); //changes b from char to String System.out.println(Character.toString(b)); } }
Listing 1.2: Demo of the Character Methods
If you run the code in Listing 1.2, you will have the output below:
true false true B b
