Character

esse quam videri
Jump to: navigation, search

Definition

A Character (or Char as used in code) is a 16-bit, Unicode character. A character can be a letter, number or symbol.

Relevance

Characters are used to make up strings. There may be times when separating strings into the characters that make them up will be useful.

Explanation

If you think of the word "Car", it is comprised of 3 characters: 'C', 'a' and 'r'. Note that a space is still a character. Thus, the word "Race Car" is comprised of 8 characters: 'R', 'a', 'c', 'e', ' ', 'C', 'a' and 'r'.

Another note is that while strings use double quotes (" ") to specify a value, characters use single quotes (' ').

Example

char firstInitial = 'J';
char lastInitial = 'D';
char favNumber = '3'; // This is still valid, the 3 will not be used for math but the character data type can hold a single digit as shown.
char favSymbol = '$'; // This is still valid. Even though the dollar sign is a symbol, it is still a character.

char badExample = "B"; // This will throw an error because the compiler will expect single quotes (' ') instead of double quotes (" "). Simply change the quotes to fix the error.
char badExample2 = 'b2'; // This will throw an error because a character can only be a single letter/ number. Having two as shown is not allowed.