The backslash (\) behaves differently

I have a little code as below

public class Testing { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String firstString = sc.next(); System.out.println("First String : " + firstString); String secondString = "text\\"; System.out.println("Second String : " + secondString); } } 

When I provide input as text \\ , I get output as

 First String : text\\ Second String : text\ 

Why do I get two different lines when the input that I provide to the first line is similar to the second line.

Demo at www.ideone.com

+4
source share
4 answers

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.

+11
source

Java uses \ as an escape character in the second line

EDITED upon request

  • In the first case, the input takes all the characters entered and encapsulates them into a String, so all characters are printed (there is no rating because they are read, they are printed).

  • In the second case, the JVM evaluates the String between " , character by character, and the first \ reads the metacharacter that protects the second, so it will not be printed.

+2
source

Lines \ use a special character, for example, you can use it like \n to create a new line character. To disable its special meaning, you need to use another \ , for example \\ . Therefore, in your second case, \\ will be interpreted as a single character \ .

If you are reading strings from external sources (e.g. streams), Java assumes that they are normal characters, because special characters have already been converted, for example, to tabs, new string characters, etc.

+2
source

The inner sequence of char strings must not be confused with the char sequence between double quotes, because the backslash has a special meaning: "\ n \ r \ t \\\ 0" => {(char) 10, (char) 13, ( char) 9, '\\', (char) 0}

0
source

All Articles