Java INDEPENDENT TOPIC 06 – CHARACTERS LESSON NOTE INTRO One
type of variable that is used less often but is useful at times is the char
variable. It holds a simple character. The
variable type char, which is short for character, can be pronounced in many
ways including kar, care, character and tchar. We will
mostly use tchar in class. CHAR LITERALS Just
like String literals are placed inside double quotes, a character literal is
places inside single quotes. VARIABLE
DECLARATION AND INITIALIZATION We
can declare a char variable named letter by using: char letter; We
can declare and initialize a char variable named let to the character b by
using: char let = 'b'; INTEGER
VALUE Each
char is associated with an integer value. We
can get the int value of a char by simply type
casting it into an int variable. char c = 'a'; System.out.println(c); //a int i = (int)c; System.out.println(i); //97
ASCII
CHART The
ascii chart gives a listing of characters and their
integer value. Click
here to see the ascii
chart. Note that the integer value is
listed as “Decimal” on the chart. Notice
that ‘a’ is 97 and ‘b’ is 98 and ‘c’ is 99 and so on… Also notice that there are different
integer values for ‘A’, ‘B’ and ‘C’.
There are also numbers for different characters including numbers. SHIFTING
CHARACTERS Given
a character, we can output the next character in the ascii
chart by type casting that char into an int, then
adding 1, then type casting it back into a char. This is called shifting. char c = 'a'; System.out.println(c); //a int i = (int)c; System.out.println(i); //97 int j = i + 1; System.out.println(j); //98 char c2 = (char)j; System.out.println(c2); //b A
shorter version of the above code would be: char c = 'a'; System.out.println(c); //a char c2 = (char)((int)c + 1); System.out.println(c2); //b Notice that
the ascii chart has two full alphabets. The difference between the int value of capital letters and lower case letters is 32. If that is unclear, see that ‘A’ is 65 and ‘a’
is 97 and the difference of the two is 32.
The same value applies for any other letter. So, we can
shift a char from upper case to its lower case version by adding 32. char c = 'T'; int i = (int)c + 32; char c2 = (char)i; System.out.println(c2); //t And of
course, we could go from lower case to upper case by subtracting 32 instead. CHARS IN IF STATEMENTS We can use
char variables in if statements like all other variables. In fact, you can use all of the common operators:
==, !=, <, <=, > and >=. char ch1 = 'A'; //int value of 65 char ch2 = 'B'; //int value of 66 System.out.println(ch1 == ch2); //false System.out.println(ch1 != ch2); //true System.out.println(ch1 < ch2); //true System.out.println(ch1 <= ch2); //true System.out.println(ch1 > ch2); //false System.out.println(ch1 >= ch2); //false We can
therefore check which letter comes first in the alphabet. char ch1 = 'T'; //int value of 84 char ch2 = 'D'; //int value of 68 if (ch1 < ch2) { System.out.println(ch1 + "
comes first."); } else { System.out.println(ch2 + "
comes first."); } The program
above outputs: “D comes first”. Warning: The
above code only works if both characters are of the same case. If one is upper case and the other is lower
case, then the upper case char will always have a smaller int
value. GETTING A CHAR FROM A
STRING All
characters in a String are numbered.
The first is numbered 0, then the next is number 1 and so on. We can use the charAt
method inside the String class to get a char that is at a specific position
in the String. String s = "Campeau"; char ch1 = s.charAt(2); System.out.println(ch1); //m The code
above will output 2, because the char m is in position 2 inside the String. THE CHARACTER CLASS Java has a
built-in Character class that provides some useful functions when working
with char variables. We will explore a
few below: CHECKING THE CASE One way to
check if a character was a lower case char would be to see if its int value was between 97 and 122 inclusively. However, there is a built-in option as
well. We can use Character.isUpperCase(char) to see if char is upper case
or not. char ch = 'G'; if (Character.isUpperCase(ch)) { System.out.println("Upper Case!"); } else { System.out.println("Lower Case!"); } Of course,
there is also Character.LowerCase(char) that
returns true if char is in lower case. CHECKING FOR A LETTER We can check
if a char is a letter by using Character.isLetter(char). char ch = '7'; if (Character.isLetter(ch)) { System.out.println("Letter!"); } else { System.out.println("Not letter!"); } CHECKING FOR A NUMBER
(DIGIT) We can check
if a char is a digit by using Character.isDigit(char). char ch = '2'; if (Character.isDigit(ch)) { System.out.println("Digit!"); } else { System.out.println("Not digit!"); } |
|