24

What is the string literal \\ backslash? What does it do? I have thought about it but I do not understand it. I also read it on wikipedia. When I try to print the following:

System.out.println("Mango \\ Nightangle");

the output is: Mango \ Nightangle

What is the significance of this string literal?

Kieran
  • 2,554
  • 3
  • 26
  • 38
Y.E.P
  • 1,187
  • 6
  • 20
  • 40
  • 8
    It is just another character. The backslash itself is an escape character so it must be escaped by itself to print just one backslash. Other than that, there is no particular significance to it. – Marko Topolnik Aug 23 '12 at 12:24
  • 1
    "What is the significance of this string literal ?" The actual String value is `Mango \ Nightangle`. `Mango \\ Nightangle` is the encoded form, the double backslash being an escape sequence where Java expects a special character. Because of this a single backslash would lead to a compilation error. – James P. Aug 23 '12 at 12:37
  • 2
    It is more common to use a forward slash in English like "Mango / Nightangle" e.g. and/or – Peter Lawrey Aug 23 '12 at 12:47

6 Answers6

34

\ is used as for escape sequence in many programming languages, including Java.

If you want to

  • go to next line then use \n or \r,
  • for tab use \t
  • likewise to print a \ or " which are special in string literal you have to escape it with another \ which gives us \\ and \"
Pshemo
  • 122,468
  • 25
  • 185
  • 269
Chandra Sekhar
  • 18,914
  • 16
  • 84
  • 125
  • what do you mean by printing a backslash ? – Y.E.P Aug 23 '12 at 12:27
  • 2
    I mean if you want to print a\z then you have to use a\\z – Chandra Sekhar Aug 23 '12 at 12:29
  • Thank you for the info. But I would like to use \n to go to next line in LocaleString. I added \n in LocaleString value inside the property file but it still doesn't support what I am trying to achieve. My goal is to create a next line in JTable header. It seems to be working in String(even with just a backslash) but not in LocaleString. Could you give advice? – In-young Choung Jul 20 '17 at 15:13
12

Imagine you are designing a programming language. You decide that Strings are enclosed in quotes ("Apple"). Then you hit your first snag: how to represent quotation marks since you've already used them ? Just out of convention you decide to use \" to represent quotation marks. Then you have a second problem: how to represent \ ? Again, out of convention you decide to use \\ instead. Thankfully, the process ends there and this is sufficient. You can also use what is called an escape sequence to represent other characters such as the carriage return (\n).

James P.
  • 19,313
  • 27
  • 97
  • 155
4

It is used to escape special characters and print them as is. E.g. to print a double quote which is used to enclose strings, you need to escape it using the backslash character.

e.g.

System.out.println("printing \"this\" in quotes");

outputs

printing "this" in quotes
Un3qual
  • 1,332
  • 15
  • 27
Vikdor
  • 23,934
  • 10
  • 61
  • 84
3

\ is used for escape sequences in programming languages.

\n prints a newline
\\ prints a backslash
\" prints "
\t prints a tabulator
\b moves the cursor one back

hvaughan3
  • 10,955
  • 5
  • 56
  • 76
akluth
  • 8,393
  • 5
  • 38
  • 42
  • In Java, trying to print \ by itself will create a compiler error. `System.out.println("Won't print \ back-slash");` gives error along lines `Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )` -- so the part `\ prints a backslash` is not true. `\\ prints a backslash` is true – Don Cheadle Jan 14 '15 at 16:50
1

The \ on it's own is used to escape special characters, such as \n (new line), \t (tabulation), \" (quotes) when typing these specific values in a System.out.println() statement.

Thus, if you want to print a backslash, \, you can't have it on it's own since the compiler will be expecting a special character (such as the ones above). Thus, to print a backslash you need to escape it, since itself is also one of these special characters, thus, \\ yields \.

npinti
  • 51,780
  • 5
  • 72
  • 96
-2

If double backslash looks weird to you, C# also allows verbatim string literals where the escaping is not required.

Console.WriteLine(@"Mango \ Nightangle");

Don't you just wish Java had something like this ;-)

Andy McCluggage
  • 37,618
  • 18
  • 59
  • 69
  • 1
    An interesting bit of information though :) . Maybe something like this is possible by extending the language. – James P. Aug 23 '12 at 12:33
  • 1
    It's tagged with java but he's not asking about Java explicitly. The wiki he posted is about escape sequences, so therefore @AndyMcCluggage is fine to post this. Not sure why it was downvoted...is it because he didn't answer the question? – cr1pto Jun 27 '17 at 03:36