W3docs

How to enter quotes in a Java string?

To enter quotes in a Java string, you can use the escape character \ to indicate that the quote is part of the string, rather than the end of the string.

To enter quotes in a Java string, you can use the escape character \ to indicate that the quote is part of the string, rather than the end of the string. The escape character \ tells the compiler to treat the character that follows it as a literal character, rather than as a special character.

Here is an example of how you can use the escape character to enter quotes in a Java string:


String str = "He said, \"Hello, World!\"";

In this example, the escape character \ is used to indicate that the quote characters " are part of the string, rather than the end of the string.

In Java, single quotes ' are exclusively used for char literals, not String literals. To include a single quote inside a double-quoted string, you must use the escape character \':

For example:


String str = "It's a nice day.";

Keep in mind that you need to use the escape character when you want to enter a quote character that is the same type as the quote character used to define the string.

I hope this helps! Let me know if you have any other questions.