The code:
String s = "\d";
raises an compiler error, illegal escape character. Ok understood!!
But the code:
class Test
{
public static void main(String[] args)
{
String s = args[0];
System.out.println(s);
}
}
does not when invoked with the command java Test \d or java Test "\d". Why??
Infact it even prints: \d without using the double back-slashes (“\\d”).
Aren’t the arguments provided via command-line treated as Strings only?
I know it can’t raise a compiler error because arguments provided at command-line are after compilation stage but then shouldn’t it raise a runtime exception or something??
Or is it that once we have passed through the compilation stage it doesn’t matter what the String contains (because the code is converted in bytecode and the whole code structure is altered)? If yes, then can someone please elaborate.
Thnx in advance!!
You don’t realise that shell command lines and Java are two different languages. A command line given to a shell is parsed as a shell command. A Java program given to
javais parsed as Java.You are possibly also confusing strings literals (pieces of code that produce strings) and strings (values consisting of a sequence of characters).
You want the two character string
\dto be stored in variables. You need to construct an appropriate string literal in order to do that.\d, one can use a cmd (Windows shell) string literal\d.\d, one can use a Java string literal"\\d".