Table of Contents

Java - Character (char)

About

character in java.

There is two type:

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

Management

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

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

Syntax

Init

Literal

Language - Literal Value value

char c = '"';

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))

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

int boxDrawAsInt =  Integer.parseInt("2514", 16);
System.out.println("Decimal Number: "+boxDrawAsInt);
Decimal Number: 9492

char boxDrawAsChar = (char) 9492;
System.out.println("Box Draw: "+boxDrawAsChar);
Box Draw: └

To

To String

To string …

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

To Int

int i = (int) '"';
int i = (int) ((Character) '"')

To Unicode code point

char c = '⡎';
System.out.println(Integer.toHexString((int) c));

which leads to the character cldr/utility/character.jsp - braille pattern dot

284e

Equality

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

Minus / Addition

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

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

Repeat

n = 10;
c = 'a'
nc = new String(new char[n]).replace("\0", c);
"a".repeat(3);

Numeric Value

getNumericValue does not return the int decimal representation of Unicode but the real numeric value of the character following this rule:

Snippet:

if (Character.isDigit(x)) {
    int y = Character.getNumericValue(x); 
    System.out.println(y);
}

Array

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;
}

Documentation / Reference