From Core Java, vol. 1, 9th ed., p. 69:
The character ? requires two code units in the UTF-16 encoding. Calling
String sentence = "? is the set of integers"; // for clarity; not in book
char ch = sentence.charAt(1)
doesn't return a space but the second code unit of ?.
But it seems that sentence.charAt(1)
does return a space. For example, the if
statement in the following code evaluates to true
.
String sentence = "? is the set of integers";
if (sentence.charAt(1) == ' ')
System.out.println("sentence.charAt(1) returns a space");
Why?
I'm using JDK SE 1.7.0_09 on Ubuntu 12.10, if it's relevant.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…