The double backslash in the console that you provide as input at runtime is indeed two backslashes. You just wrote backslash ASCII characters twice.
A double backslash inside a string literal means only one backslash. Because you cannot write a single backslash in a string literal. What for? Because the backslash is a special character that is used to โturn offโ special characters. For example: tab, new line, backslash, double quotation mark. As you can see, the backslash is also one of the characters to be avoided. How do you avoid? With a backslash. Thus, overcoming the backslash is done by putting it back in the backslash. Thus, this results in two backslashes. This will be compiled into one backslash.
Why do you need to avoid characters? Look at this line: this "is" a string . If you want to write this as a string literal in Java, you may deliberately think that it will look like this:
String str = "this "is" a string";
As you can see, this will not compile. Therefore, avoid them like this:
String str = "this \"is\" a string";
At the moment, the compiler knows that " does not close the line, but actually means a character " , because you avoid it with a backslash.
source share