We have ever seen characters of primitive char data type.
But java also provides a wrapper class called Character class. The
Character class wraps a char value in an object. We can create objects of
Character class using new keyword. The Character class contains a single field
whose type is char. We can manipulate characters using many static methods
provides by Character class.
Creating an Object of Character Class
Character ch = new Character('A');
The Character class has only one constructor that contains
only one argument of char value.
The java compiler has a feature called autoboxing or unboxing, under this
feature, if we pass a primitive char into a method that expects an object,
the compiler automatically converts the char to a Character class object.
Escape Sequences
Any character preceded by a backslash \
character is escape sequence. Escape sequence character has special meaning to
the compiler.
This 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. |
We can use escapes sequences in print statement. When java compiler encounters escape sequences in print statement, the compiler interprets it accordingly.
Example of Escape Sequences
class EscapeSequence
{
public static void main(String[] args) {
System.out.print("\tWelcome to \"Pc Technical Pro\"");
}
}
Output:
Welcome to "Pc Technical Pro"
Character Class Methods
1. isLetter(char ch)
The isLetter(char ch) methods returns Boolean value. This method returns
true if specified char value is letter, otherwise it returns false. We can also
pass ASCII value of char value to this method, as typecasting is implicitly
performed by compiler.
Example:
class CharacterMethods
{
public static void main(String[] args) {
System.out.println(Character.isLetter('B'));
System.out.println(Character.isLetter('6'));
}
}
Output:
true
false
2. isDigit(char ch)
The isDigit(char ch) methods returns Boolean
value. This method returns true if specified char value is digit,
otherwise it returns false. We can also pass ASCII value of char value to
this method.
Example:
class CharacterMethods
{
public static void main(String[] args) {
System.out.println(Character.isDigit('B'));
System.out.println(Character.isDigit('6'));
}
}
Output:
false
true
3. isWhitespace(char ch)
This method determines that specified
char value is whitespace or not. The whitespace includes space, tab, or
new line.
Example:
class CharacterMethods
{
public static void main(String[] args) {
System.out.println(Character.isWhitespace('B'));
System.out.println(Character.isWhitespace('6'));
System.out.println(Character.isWhitespace(' '));
System.out.println(Character.isWhitespace('\n'));
System.out.println(Character.isWhitespace('\t'));
}
}
Output:
false
false
true
true
true
4. isUpperCase(char ch)
This method determines whether the
specified char value is uppercase or not.
Example:
class CharacterMethods
{
public static void main(String[] args) {
System.out.println(Character.isUpperCase('a'));
System.out.println(Character.isUpperCase('B'));
System.out.println(Character.isUpperCase('6'));
}
}
Output:
false
true
false
5. isLowerCase(char ch)
This method determines whether the
specified char value(ch) is lowercase or not.
Example:
class CharacterMethods
{
public static void main(String[] args) {
System.out.println(Character.isLowerCase('a'));
System.out.println(Character.isLowerCase('B'));
System.out.println(Character.isLowerCase('6'));
}
}
Output:
true
false
false
0 comments:
Post a Comment