Escaped sequences are not transformed by command shells into their corresponding character codes (How can I echo a newline in a batch file?), so what you are wondering is why the java
program doesn't do some massaging of the parameters received when invoking it. Well, the reason is simple: the massaging is arbitrary, maybe some user is not expecting a string to be interpreted as human text; but a string that represents other things where the arbitrary transformation of escaped codes is a failed generalization. Some examples:
- The strings are windows file paths like
c:\my\folder\number\n
, there you can find two \n
that if java would arbitrarily generalized as human text would be making a major mistake.
- Literal
ids
, passwords, ascii art,
- Structured representation of serializable content like xml, model object, proprietary content, etc.
Now, if you define that string INSIDE java code that is going to be compiled, after compilation all \?
will be compiled into their corresponding escape code (as a feature of the language); but you can tell the java compiler to do not do this by escaping the escape, i.e. \\?
; but this compilation concerns. At runtime all strings are nothing more than char[]
and no arbitrary massaging is applied to them.
Check the JLS:
It is a compile-time error if the character following a backslash in an escape is not an ASCII b, t, n, f, r, ", ', \, 0, 1, 2, 3, 4, 5, 6, or 7. The Unicode escape \u is processed earlier (§3.3).