Characters

0
12

This Java Tutorial is all about Characters.
Characters
Most of the time when we work with single character value we use primitive type char.
Example:
Char x=’Z’;
However ,sometimes we need to use a char as an object.For this purpose Java provides us wrapper class that wraps char to Character object.A Character object can be created with a Character constructor.
Character c = new Character(‘Z’);

Example:

  1. public class Test {
  2.  
  3. public static void main(String[] args) {
  4.  
  5. char ch1 = 'z';
  6. char ch2 = 90; /* ASCII code of 'Z'*/
  7.  
  8. System.out.println("Value of char variable ch1 is :" + ch1);
  9. System.out.println("Value of char variable ch2 is :" + ch2);
  10. }
  11. }

Output:
Value of char variable ch1 is :z
Value of char variable ch2 is :Z

Under some circumstances,Java compiler creates a Character object.For example, if we pass a primitive char into a method that expects an object, the compiler automatically converts the char to a Character. This feature is called autoboxing.If the conversion goes the other way ,it is called unboxing.

Character methods
s.no. Methods Description
1. isLetter() Checks whether the given char value is a letter.
2. isDigit() Checks whether the given char value is a digit.
3. isWhitespace() Checks whether the given char value is white space.
4. isUpperCase() Checks whether the given char value is in upper case.
5. isLowerCase() Checks whether the given char value is in lower case.
6. toUpperCase() Returns upper case of the specified char value.
7. toLowerCase() Returns the lower case of the specified char value.

Escape sequences

These are the characters preceded by a backslash(\) and have a special meaning to a compiler.
Escape Sequence Description
\n                              newline
\t                                tab
\b                               backspace
\f                                form feed
\r                                return
\” ”                             (double quote)
\’ ‘                              (single quote)
\\ \                             (back slash)
\uDDDD                    character from the Unicode character set (DDDD is four                                      hex digits)

Facebook Comments
(Visited 6 times, 1 visits today)

LEAVE A REPLY

Please enter your comment!
Please enter your name here