Escape sequences in command line arguments (Java)

…衆ロ難τιáo~ 提交于 2021-01-01 04:30:23

问题


According to How to use line break argument

When you define a String as "Hello\nHello" in Java, it contains no '\' character. It is an escape sequence for the line break: "\n" is just one character.

When you use this string as an argument to your program, however (so the string is defined outside), "\n" is interpreted as two characters: '\' and 'n'.

Why won't the command line arguments containing escape sequences be compiled as well? I thought the command line arguments are placed into the array String[] args?

And String[] args will contain args[0] = "Hello\nJava";


回答1:


Command-line arguments are not Java source code, so the rules for the meaning of characters in Java source code do not apply.

Interpretation or otherwise of command-line arguments is the province of the command interpreter; Java is not treated specially. For example, \n is not substituted inside a quoted-string in most (all?) Linux shells:

  $ echo 'a \n b'
  a \n b

Outside of quotes, backslash-n means 'literally n', which is just the same as 'n' since 'n' is not special in any way to the shell.

 $ echo a\nb
 anb

Sure, the Java system could apply its own processing after the command interpreter, but most Linux users would find that confusing; Java commands would act inconsistently compared to other commands.




回答2:


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:

  1. 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.
  2. Literal ids, passwords, ascii art,
  3. 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).



来源:https://stackoverflow.com/questions/56195108/escape-sequences-in-command-line-arguments-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!