W3docs

How can I make Java print quotes, like "Hello"?

To print quotes in Java, you need to use the escape character \ to indicate that the quote is part of the string and not the end of the string.

To print quotes in Java, you need to use the escape character \ to indicate that the quote is part of the string and not the end of the string.

Here is an example of how you can print a quote in Java:


System.out.println("\"Hello\"");  // prints "Hello"

Alternatively, you can use a char literal with single quotes to represent a single quotation mark, since Java strings must use double quotes:


char q = '"';
System.out.println(q + "Hello" + q);  // prints "Hello"

You can also use the printf() method to print a quote, using the %s format specifier:


System.out.printf("\"%s\"", "Hello");  // prints "Hello"

Keep in mind that you need to use the escape character \ whenever you want to print a quote or any other special character that has a special meaning in Java (such as \n for a new line or \t for a tab).

For example, to print a quote and a new line, you can use the following code:


System.out.println("\"Hello\"\n");  // prints "Hello" and a new line