What is the difference between char and String in Java, and when would you choose each one?
A char is a primitive: a single unsigned 16-bit UTF-16 code unit, stored inline, comparable with ==, and never null. A String is a class: an object holding a sequence of characters, compared by value with equals, and possibly null.
The comparison semantics are the part that trips people up. char is a primitive, so c1 == c2 compares the two values directly and does exactly what you expect. A String is a reference, so s1 == s2 compares identity, and only tells you whether the two variables point at the same object. Two Strings holding the same letters are usually different objects, so == says false.
Where you reach for a char: a single character from user input, a loop variable over a short alphabet, a code point in a switch, a flag. Where you reach for a String: anything that is text rather than a symbol — a name, a line, a key. A String of length one is still a String, and using it where a char is expected requires a charAt call.
One wrinkle: Java's char is a UTF-16 code unit, not a full character. Any character outside the Basic Multilingual Plane — most emoji, some historic scripts, some musical symbols — is stored as a surrogate pair of two chars. So a char cannot represent every character a user might type, and code that counts characters with a char counter will undercount. Where that matters, use codePointCount or the code-point APIs.