|
1 | 1 | # About
|
2 | 2 |
|
3 |
| -`char`s are generally easy to use. |
4 |
| -They can be extracted from strings, added back (by means of a string builder), defined and initialised using literals with single quotes, as in `char ch = 'A';`, assigned and compared. |
| 3 | +The Java `char` primitive type is a 16 bit representation of a single Unicode character. |
5 | 4 |
|
6 |
| -The Character class encapsulates the char value. |
| 5 | +~~~~exercism/note |
| 6 | +The `char` type is based on the [original Unicode specification][unicode-specification], which used 16 bits to represent characters. |
| 7 | +This is enough to cover most of the common letters and covers characters in the range 0x0000 to 0xFFFF. |
| 8 | +The specification has since expanded the range of possible characters up to 0x01FFFF. |
| 9 | +
|
| 10 | +[unicode-specification]: https://www.unicode.org/versions/Unicode1.0.0/ |
| 11 | +~~~~ |
| 12 | + |
| 13 | +Multiple `char`s can comprise a string, such as `"word"`, or `char`s can be processed independently. |
| 14 | +A `char` literal is surrounded by single quotes (e.g. `'A'`). |
| 15 | + |
| 16 | +```java |
| 17 | +char lowerA = 'a'; |
| 18 | +char upperB = 'B'; |
| 19 | +``` |
| 20 | + |
| 21 | +## Getting the `char`s of a `String` |
| 22 | + |
| 23 | +The `String.toCharArray` method returns a String's chars as an array. |
| 24 | +As mentioned in [arrays][concept-arrays], you can use a `for` loop to iterate over the array. |
| 25 | + |
| 26 | +```java |
| 27 | +String text = "Hello"; |
| 28 | +char[] asArray = text.toCharArray(); |
| 29 | + |
| 30 | +for (char ch: asArray) { |
| 31 | + System.out.println(ch); |
| 32 | +} |
| 33 | + |
| 34 | +// Outputs: |
| 35 | +// H |
| 36 | +// e |
| 37 | +// l |
| 38 | +// l |
| 39 | +// o |
| 40 | +``` |
| 41 | + |
| 42 | +## The [Character][docs-character] class |
| 43 | + |
| 44 | +There are many builtin library methods to inspect and manipulate `char`s. |
| 45 | +These can be found as static methods of the [`java.lang.Character`][docs-character] class. |
| 46 | +Here are some examples: |
| 47 | + |
| 48 | +```java |
| 49 | +Character.isWhitespace(' '); // true |
| 50 | +Character.isWhitespace('#'); // false |
| 51 | + |
| 52 | +Character.isLetter('a'); // true |
| 53 | +Character.isLetter('3'); // false |
| 54 | + |
| 55 | +Character.isDigit('6'); // true |
| 56 | +Character.isDigit('?'); // false |
| 57 | +``` |
| 58 | + |
| 59 | +~~~~exercism/note |
| 60 | +Some methods in the Character class have an overload so that it can take either an `char` or `int`. |
| 61 | +For example, `isDigit` has one that accepts a [`char`][is-digit-char] and another an [`int`][is-digit-int]. |
| 62 | +As mentioned earlier, the `char` type can only represent the characters in the range from 0x0000 to 0xFFFF. |
| 63 | +The `int`, however, can represent all characters, hence the `int` overloads. |
| 64 | +
|
| 65 | +[is-digit-char]: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/Character.html#isDigit(char) |
| 66 | +[is-digit-int]: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/Character.html#isDigit(int) |
| 67 | +~~~~ |
| 68 | + |
| 69 | +## Adding a `char` to a `String` |
| 70 | + |
| 71 | +The `+` operator can be used to add a `char` to a `String`. |
| 72 | + |
| 73 | +```java |
| 74 | +'a' + " banana" // => "a banana" |
| 75 | +"banana " + 'a' // => "banana a" |
| 76 | +``` |
| 77 | + |
| 78 | +~~~~exercism/caution |
| 79 | +Becareful _not_ to use `+` to join two `char`s together to form a `String`! |
| 80 | +Adding two `char`s this way gives an `int`, _not_ a `String`! |
| 81 | +For example: |
| 82 | +
|
| 83 | +```java |
| 84 | +'b' + 'c'; |
| 85 | +// => 197 (not the String "bc") |
| 86 | +``` |
| 87 | +
|
| 88 | +This is because Java promotes the `char` to an `int` (see [4.2 Primitive Types and Values ][jls-primitives] of the [Java Language Specification][jls-main]). |
| 89 | +
|
| 90 | +[jls-main]: https://docs.oracle.com/javase/specs/jls/se21/html/ |
| 91 | +[jls-primitives]: https://docs.oracle.com/javase/specs/jls/se21/html/jls-4.html#jls-4.2 |
| 92 | +~~~~ |
| 93 | + |
| 94 | +However, when there are many characters to be added, it can be more efficient to use a [`StringBuilder`][docs-stringBuilder] instead: |
| 95 | + |
| 96 | +```java |
| 97 | +StringBuilder builder = new StringBuilder(); |
| 98 | +builder.append('a'); |
| 99 | +builder.append('b'); |
| 100 | +builder.append('c'); |
| 101 | + |
| 102 | +String builtString = builder.toString(); |
| 103 | +// => abc |
| 104 | +``` |
| 105 | + |
| 106 | +[concept-arrays]: https://exercism.org/tracks/java/concepts/arrays |
| 107 | +[docs-character]: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/Character.html |
| 108 | +[docs-stringBuilder]: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/StringBuilder.html |
0 commit comments