2

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

csguy
  • 1,195
  • 1
  • 8
  • 24

2 Answers2

4

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.

  • so String[] args stores the string that are processed by the command interpreter – csguy May 18 '19 at 01:58
  • 1
    Right. The command interpreter breaks the command line into arguments according to its own rules (fairly standard between shells by this point in time) and passes those into the created process. If that process is running a Java virtual machine then the JVM may pass some of those arguments on to the main() function unchanged; I don't know whether that is required by the JVM specification, but it is empirically true of the Oracle implementation on Linux. –  May 18 '19 at 02:15
0

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).

Marco R. - Bopsys LLC
  • 2,484
  • 12
  • 31