Escape sequences in command line arguments (Java)

May 18, 2019, at 7:40 PM

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

Answer 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

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.

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

One database editable by multiple machines

One database editable by multiple machines

I am new to coding at this level, and was wondering how I would get a database that multiple users on different networks could edit

Prevent Negative numbers for Age without using client side validation

Prevent Negative numbers for Age without using client side validation

I have an issue in Core javaConsider the Employee class having an attribute called age

Write a function inside a jquery if an option of select field is selected

Write a function inside a jquery if an option of select field is selected

While editing a podcast, I am getting an option being selected through the PHP code and now I want to write a j-query if the option is selected from the select field