Character Class

Character class is a wrapper class for char primitive data type. Situation where we want to use char variable as object, wrapper class is used i.e. we want to use char with ArrayList then we have to go for Character wrapper class.

Constructor:

  Character(char ch)

Creating Character object:


Character ch=new Character('a');
  

Some Useful Methods of Character class

Method Description
static boolean isLetter(char ch) Determine if specified character is letter or not.
static int digit(char ch, int radix) Return numerical value of specified character in specified radix
static boolean isLowerCase(char c) return true if value of this Character's object is in lower case otherwise return false
static boolean isUpperCase(char c) return true if value of this Character's object is in Upper case otherwise return false
char charValue() Returns the value of this character object.
int compareTo(Character c) Compare two Character object numerically.
boolean equals(Object ob) Compare this object against specified object.
String toString() Return a String obejct representing this object's value.
int hasCode() Return a hash code for this Character object.


Example



class wrap 
{
public static void main(String arg[])
{

Character ch=new Character('x');
char a=ch.charValue();

//********* Checking for letter
System.out.println(a+" is letter :"+Character.isLetter(ch));

//********* Checking for digit
System.out.println(a+" is Digit :"+Character.isDigit(ch));

//********* Checking for Lower case
System.out.println(a+" in lower case :"+Character.isLowerCase(ch));

//********* Checking for Upper case
System.out.println(a+" in upper case :"+Character.isUpperCase(ch));

//********* Converting into string
String s=ch.toString();
System.out.println("in String format:"+s);

//********* Comparing two Character's object
Character d=new Character('y');
char b=d.charValue();
System.out.println(a+" and "+b+" comparison:"+ch.compareTo(d));
}
}
/*Output:
x is letter :true
x is Digit :false
x in lower case :true
x in upper case :false
in String format:x
x and y comparison:-1
*/