36

In Java, \' denotes a single quotation mark (single quote) character, and \" denotes a double quotation mark (double quote) character.

So, String s = "I\'m a human."; works well.

However, String s = "I'm a human." does not make any compile errors, either.

Likewise, char c = '\"'; works, but char c = '"'; also works.

In Java, which is better to use? In HTML or CSS, things like style="font-family:'Arial Unicode MS';" are more often (and for such tags, I think it's the only way to use quotation marks), but in Java, I usually saw people use escape characters like "I\'m a human."

Naetmul
  • 14,544
  • 8
  • 57
  • 81
  • 3
    I would not use slash unless it's warranted. So `"I'm a human."` is ok. – vikingsteve May 21 '13 at 07:12
  • I'm not quite sure about single quotes in Strings since im not consequent using one over another there. But i recommend escaping double quotes in characters because sometimes you later decide to change what you want to do and change your character variable to string. i.e. when you find out that you want to replace more than one character with `String.replace()` – Marco Forberg May 21 '13 at 07:15
  • 1
    Have you tried testing it? – Danubian Sailor May 21 '13 at 07:25
  • There’s also the option of using the proper English quotation marks when the string is in English, as in `'I’m a human'`. Such marks never need to be escaped, not matter what string delimiters are used. – Jukka K. Korpela May 21 '13 at 07:56

2 Answers2

55

You don't need to escape the ' character in a String (wrapped in "), and you don't have to escape a " character in a char (wrapped in ').

Cory Kendall
  • 7,195
  • 8
  • 37
  • 64
  • 1
    but you can and it is legal, is what you're saying, right? – Mishax Nov 19 '15 at 09:14
  • 1
    @Mishax it's quite obvious. The only use of the escape character is to **tell** the compiler to not treat the character('/") as the closing character. – Zaid Khan Oct 03 '16 at 15:12
  • 2
    @Bateman I disagree with your statement - the escape character can be used in a String literal with \n to indicate a newline or a \t for a tab. My hint to Cory Kendall was that his answer did not use formal terms like "legal but optional" which would have been more precise. – Mishax Oct 04 '16 at 06:17
15

It's best practice only to escape the quotes when you need to - if you can get away without escaping it, then do!

The only times you should need to escape are when trying to put " inside a string, or ' in a character:

String quotes = "He said \"Hello, World!\"";
char quote = '\'';
Jamie
  • 3,890
  • 3
  • 26
  • 35
  • 17
    Umm ... where does it say that this is "best practice"? Or are you just relabelling your personal preferences as "best practice"? – Stephen C May 21 '13 at 08:02
  • for example, i have found it useful when im doing search and replace across a large file. today i want to replace \" -> \' tomorrow i may want to do the opposite. but ' -> \" may end up matching cases i do not want to replace – acid_crucifix Nov 19 '19 at 14:59
  • @StephenC Readability is a best practice :) I'll leave it at that. – Gili Jun 23 '22 at 18:43
  • @Gili - You need to read this: [No Best Practices](https://www.satisfice.com/blog/archives/5164) - It starts: *"Dear Reader, I would like you to give up, henceforth, the idea of “best practice.” Thank you."* – Stephen C Jun 23 '22 at 22:41