Download java lang Character Class in Formats
QR Code for this Page
java lang Character Class

java lang Character Class

java.lang.Character Class

Character Class in Java

The Java Character class wraps the value of primitive data type char into Java 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.

Java Character class provides a large number of static methods for determining a character's category (lowercase letter, digit, etc.) and for converting characters from uppercase to lowercase and vice versa.

Unicode Conformance in Java

The fields and methods of class Character are defined in terms of character information from the Unicode Standard, specifically the UnicodeData file that is part of the Unicode Character Database. This file specifies properties including name and category for every assigned Unicode code point or character range. The file is available from the Unicode Consortium at http://www.unicode.org.

The Java SE 12 Platform uses character information from version 11.0 of the Unicode Standard, plus the Japanese Era code point, U+32FF, from the first version of the Unicode Standard after 11.0 that assigns the code point.

Unicode Character Representations in Java

The char data type (and therefore the value that a Character object encapsulates) are based on the original Unicode specification, which defined characters as fixed-width 16-bit entities. The Unicode Standard has since been changed to allow for characters whose representation requires more than 16 bits. The range of legal code points is now U+0000 to U+10FFFF, known as Unicode scalar value. (Refer to the definition of the U+n notation in the Unicode Standard.)

The set of characters from U+0000 to U+FFFF is sometimes referred to as the Basic Multilingual Plane (BMP). Characters whose code points are greater than U+FFFF are called supplementary characters. The Java platform uses the UTF-16 representation in char arrays and in the String and StringBuffer classes. In this representation, supplementary characters are represented as a pair of char values, the first from the high-surrogates range, (\uD800-\uDBFF), the second from the low-surrogates range (\uDC00-\uDFFF).

A char value, therefore, represents Basic Multilingual Plane (BMP) code points, including the surrogate code points, or code units of the UTF-16 encoding. An int value represents all Unicode code points, including supplementary code points. The lower (least significant) 21 bits of int are used to represent Unicode code points and the upper (most significant) 11 bits must be zero. Unless otherwise specified, the behavior with respect to supplementary characters and surrogate char values is as follows:

  • The methods that only accept a char value cannot support supplementary characters. They treat char values from the surrogate ranges as undefined characters. For example, Character.isLetter('\uD840') returns false, even though this specific value if followed by any low-surrogate value in a string would represent a letter.
  • The methods that accept an int value support all Unicode characters, including supplementary characters. For example, Character.isLetter(0x2F81A) returns true because the code point value represents a letter (a CJK ideograph).

In the Java SE API documentation, Unicode code point is used for character values in the range between U+0000 and U+10FFFF, and Unicode code unit is used for 16-bit char values that are code units of the UTF-16 encoding. For more information on Unicode terminology, refer to the Unicode Glossary.

java.lang.Character Class Example

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

public class CharacterExample
{
	public static void main(String args[])
	{
		//JAVA CHARACTER DECLARATION

		Character c;

		//MEMORY ALLOCATION FOR JAVA CHARACTER

		c = new Character('A');

		//JAVA CHARACTER CLASS OUTPUT

		System.out.println("Java Character Class Example");

		//RETURNS char PRIMITIVE DATA TYPE

		System.out.println("Value is : "+ c.charValue());
	}
}

java.lang.Character Class Example 2

/* Java Character Class Example 2 */
/* Save with file name CharacterExample2.java */

public class CharacterExample2
{
	public static void main(String args[])
	{
		//JAVA CHARACTER DECLARATION

		Character c, c2;

		//PRIMITIVE char DECLARATION VALUE ASSIGNMENT

		char a = 65, b = 97;

		//MEMORY ALLOCATION FOR JAVA CHARACTER

		c = new Character(a);

		c2 = new Character(b);

		//JAVA CHARACTER CLASS OUTPUT

		System.out.println("Java Character Class Example");

		System.out.println("c Value is : "+ c.charValue());

		System.out.println("c2 Value is : "+ c2.charValue());

		System.out.println("c compareTo c2 : "+ c.compareTo(c2));

		System.out.println("c equals c2 : "+ c.equals(c2));
	}
}

Following Java Character class example you can learn how to use static methods of Java 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

/* Java 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("Java Character Class Example");

		//RETURN Java 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'));
	}
}
Download java lang Character Class in Formats