A few days ago I was asked about this program exit:
public static void main(String[] args) {
My first thought was that this program should print a length a\u0022.length() + \u0022b , which is 16 , but it is surprising that it printed 2 . I know that \u0022 is unicode for " , but I thought that this " would be escaped and would represent only one " literal without any particular meaning. And actually Java somehow parsed this line like this:
System.out.println("a".length() + "b".length());
I can not plunge into this strange behavior. Why don't Unicode screens behave like regular escape sequences?
Update . Apparently, this was one of the brain trainers of Java Puzzlers: Traps, Traps, and Corner Cases, a book written by Joshua Bloch and Neil Gafter. More specifically, the question was related to puzzle 14: Escape Rout.
source share