java.lang.Character Class

The Character class wraps the value of primitive data type char into Character object. An object of type Character contains a single field whose type is char. In other words the wrapper classes create objects for primitive data types. The wrapper classes are Boolean, Byte, Character, Short, Integer, Float, Long and Double.

This class provides several methods for determining a character's category (lowercase letter, digit, etc.) and for converting characters from uppercase to lowercase and vice versa.

This class has following constructors.

Character Class Constructors

Character(char value)

Constructs a newly allocated Character object that represents the specified char value.

java.lang.Character Class Example
/* Save with file name CharacterExample.java */

public class CharacterExample
{
	public static void main(String args[])
	{
		//CHARACTER DECLARATION
		Character c;
		//MEMORY ALLOCATION FOR CHARACTER
		c = new Character('A');
		//CHARACTER CLASS OUTPUT
		System.out.println();
		System.out.println("Character Class Example");
		System.out.println("=====================");
		System.out.println();
		//RETURNS char PRIMITIVE DATA TYPE
		System.out.println("Value is : "+ c.charValue());
	}
}
                        
java.lang.Character Class Example 2
/* Character Class Example 2 */
/* Save with file name CharacterExample2.java */

public class CharacterExample2
{
	public static void main(String args[])
	{
		//CHARACTER DECLARATION
		Character c, c2;
		//PRIMITIVE char DECLARATION VALUE ASSIGNMENT
		char a = 65, b = 97;
		//MEMORY ALLOCATION FOR CHARACTER
		c = new Character(a);
		c2 = new Character(b);
		//CHARACTER CLASS OUTPUT
		System.out.println();
		System.out.println("Character Class Example");
		System.out.println("=====================");
		System.out.println();
		System.out.println("c Value is : "+ c.charValue());
		System.out.println("c2 Value is : "+ c2.charValue());
		System.out.println();
		System.out.println("c compareTo c2 : "+ c.compareTo(c2));
		System.out.println("c equals c2 : "+ c.equals(c2));
	}
}
                        

In the following example you can learn how to use static methods of Character Class. If you use static methods you need not to create the class instance. You can call static methods using the class name as reference.

java.lang.Character Class Example 3
/* Save with file name CharacterExample3.java */

public class CharacterExample3
{
	public static void main(String args[])
	{
		//PRIMITIVE char DECLARATION VALUE ASSIGNMENT
		char a = 65;
		//CHARACTER CLASS STATIC METHODS USAGE
		System.out.println("Character Class Example");
		System.out.println("=====================");
		System.out.println();
		//RETURN Character OBJECT WITH THE SPECIFIED PRIMITIVE char DATA TYPE
		System.out.println("valueOf : "+ Character.valueOf(a));
		System.out.println("A isDigit : "+ Character.isDigit('A'));
		System.out.println("A isLetter : "+ Character.isLetter('A'));
		System.out.println("A isLowerCase : "+ Character.isLowerCase('A'));
		System.out.println("A isUpperCase : "+ Character.isUpperCase('A'));
		System.out.println("toLowerCase : "+ Character.toLowerCase('A'));
		System.out.println("toUpperCase : "+ Character.toUpperCase('A'));
	}
}