Character in Java
Java provides a wrapper class Character in
java.lang package. An object of type Character contains a single field, whose
type is char.
· Creating a Character object :
Character ch=new Character(‘a’);
The above statement creates a Character
object which contain ‘a’ of type char. There is only one constructor in
Character class which
·
If
we pass a primitive char into a method that expects an object, the compiler
automatically converts the char to a Character class object. This feature is
called Autoboxing and Unboxing.
·
Note : The Character class is immutable like
String class i.e once it’s object is created, it cannot be
changed.
how to read charecter using all class |
Methods in Character Class :
1. boolean isLetter(char ch) : This method is used
to determine whether the specified char value(ch) is a letter or not. The
method will return true if it is letter([A-Z],[a-z]), otherwise return false.
In place of character, we can also pass ASCII value as an argument as
char to int is implicitly typecasted in java.
Syntax :
boolean isLetter(char ch)
Parameters :
ch - a primitive character
Returns :
returns true if ch is a alphabet, otherwise
return false
Example:
//
Java program to demonstrate isLetter() method
public
class Test
{
public
static void main(String[] args)
{
System.out.println(Character.isLetter('A'));
System.out.println(Character.isLetter('0'));
}
}
|
Output:
true
false
2. boolean isDigit(char ch) :
This method is used to determine whether the specified char value(ch) is a
digit or not. Here also we can pass ASCII value as an argument.
Syntax :
boolean isDigit(char ch)
Parameters :
ch - a primitive character
Returns :
returns true if ch is a digit, otherwise
return false
Example:
//
Java program to demonstrate isDigit() method
public
class Test
{
public
static void main(String[] args)
{
//
print false as A is character
System.out.println(Character.isDigit('A'));
System.out.println(Character.isDigit('0'));
}
}
|
Output:
False
True
3. boolean isWhitespace(char ch) : It
determines whether the specified char value(ch) is white space. A whitespace
includes space, tab, or new line.
Syntax :
boolean isWhitespace(char ch)
Parameters :
ch - a primitive character
Returns :
returns true if ch is a whitespace.
otherwise return false
//
Java program to demonstrate isWhitespace() method
public
class Test
{
public
static void main(String[] args)
{
System.out.println(Character.isWhitespace('A'));
System.out.println(Character.isWhitespace('
'));
System.out.println(Character.isWhitespace('\n'));
System.out.println(Character.isWhitespace('\t'));
//ASCII
value of tab
System.out.println(Character.isWhitespace(9));
System.out.println(Character.isWhitespace('9'));
}
}
|
Output:
False
True
True
True
True
False
4. boolean isUpperCase(char
ch) : It determines whether the specified char value(ch) is
uppercase or not.
Syntax :
boolean isUpperCase(char ch)
//
Java program to demonstrate isUpperCase() method
public
class Test
{
public
static void main(String[] args)
{
System.out.println(Character.isUpperCase('A'));
System.out.println(Character.isUpperCase('a'));
System.out.println(Character.isUpperCase(65));
}
}
|
Output:
true
false
true
5. boolean isLowerCase(char
ch) : It determines whether the specified char value(ch) is
lowercase or not.
Syntax :
boolean isLowerCase(char ch)
Example:
//
Java program to demonstrate isLowerCase() method
public
class Test
{
public
static void main(String[] args)
{
System.out.println(Character.isLowerCase('A'));
System.out.println(Character.isLowerCase('a'));
System.out.println(Character.isLowerCase(97));
}
}
|
Output:
False
True
False
No comments:
Post a Comment