Java - Character (char)

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.

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

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

4.2.3 - To Unicode code point


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

which leads to the character 284e - braille pattern dot


284e

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

  • General

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

  • From java 11 as the repeat function

"a".repeat(3);

4.6 - Numeric Value

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

  • 0 to 9 for the digits
  • then 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')

Snippet:


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

4.7 - Array

4.7.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


Data Science
Data Analysis
Statistics
Data Science
Linear Algebra Mathematics
Trigonometry

Powered by ComboStrap