Java - Character (char)

> Procedural Languages > Java

1 - About

character in java.

There is two type:

They are only manipulated in unicode because the Java platform stores character values using Unicode conventions.

Advertising

3 - Management

The Character class wraps a char variable but also provides several management methods such as:

  • determining a character's category (lowercase letter, digit, etc.)
  • or converting characters from uppercase to lowercase and vice versa.

Character information is based on the Unicode Standard, version 6.0.0 and is based on the data file.

4 - Syntax

4.1 - Init

4.1.1 - Literal

Language - Literal Value value

  • A char is quoted in single quote
char c = '"';

4.1.2 - Hexadecimal (Unicode)

literal notation where the unicode code point (heaxdecimal) starts with a \u. Example with the box drawing character U+2514

char c = '\u2514';
((char) Integer.parseInt("2514", 16))
Advertising

4.1.3 - Integer (Unicode)

Java uses an int internaly (and not a hexadecimal) to store an unicode character, therefore you can cast the integer represention of the unicode hexadecimal representation to get a character.

Example with the box drawing character U+2514

  • 2514 is equivalent to the number 9492, proof:
int boxDrawAsInt =  Integer.parseInt("2514", 16);
System.out.println("Decimal Number: "+boxDrawAsInt);
Decimal Number: 9492
  • You can cast it directly
char boxDrawAsChar = (char) 9492;
System.out.println("Box Draw: "+boxDrawAsChar);
Box Draw: └

4.2 - To

4.2.1 - To String

To string …

String.valueOf((char) Integer.parseInt("2514", 16))

4.2.2 - To Int

  • for a char
int i = (int) '"';
  • it works also from a character
int i = (int) ((Character) '"')
Advertising

4.3 - Equality

char c = '\u0000';
char d = 0;
if (c == d){
     System.out.println("They are the same");
}

4.4 - Minus / Addition

Because internally a character is an integer, you can do all integer mathematics operations.

range = (char) this.max - (char) this.min

4.5 - Numeric Value

NumericValue is a numeric value independent of the Unicode specification and is not the int decimal representation of Unicode. See java/lang/Character#getNumericValue

The characters below have a numeric values from 10 through 35.

  • The letters A-Z in their uppercase ('\u0041' through '\u005A'),
  • lowercase ('\u0061' through '\u007A'),
  • and full width variant ('\uFF21' through '\uFF3A' and '\uFF41' through '\uFF5A')
int i = Character.getNumericValue((Character) myCharacter)

4.6 - Array

4.6.1 - Contains

char[] endOfLineCharacters = {(char) 10, (char) 13};
String.valueOf(endOfLineCharacters).contains(String.valueOf((char) 10)));
boolean contains(char[] charArray, char c){
    boolean contains = false;
    for (char cA : charArray) {
       if (cA == c) {
          contains = true;
          break;
       }
    }
    return contains;
}

5 - Documentation / Reference