Chapter 1.3: Text in Code - Characters and Strings
Having worked with numbers, let’s explore how computers handle text. While we see letters and symbols, computers only understand numbers. We’ll uncover how this works through two key types:
Characters: Single text units like ‘A’ or ‘7’
Strings: Sequences of characters like “Hello”
Characters
When you type a letter, the computer assigns it a number. Just like we used integers to count things, we use characters to represent text symbols. Each character has its own numeric identity.
Chars 🚗
Char
A Char is a 16-bit value type representing a single text character with a value between 0 and 65,535.
Properties
Each char maps to exactly one number
Numbers 0-127 match ASCII standard
Numbers 128-65535 allow international characters
Example
'A'; // A char 'A', char literals are wrapped in single quotes'5'; // Numbers can be characters too, when wrapped in single quotes'$'; // So can symbols
Character Arithmetic
Just like we added and subtracted integers, we can do math with characters using their numeric values.
Character Operations
Character Addition
Add:(char,int)→int returns the numeric result of adding a character’s code and an integer