Escape sequences in command line arguments (Java)

csguy :

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";

user10762593 :

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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=74812&siteId=1