How can I make Java print quotes, like "Hello"?
Solution 1:
System.out.print("\"Hello\"");
The double quote character has to be escaped with a backslash in a Java string literal. Other characters that need special treatment include:
- Carriage return and newline:
"\r"
and"\n"
- Backslash:
"\\"
- Single quote:
"\'"
- Horizontal tab and form feed:
"\t"
and"\f"
The complete list of Java string and character literal escapes may be found in the section 3.10.6 of the JLS.
It is also worth noting that you can include arbitrary Unicode characters in your source code using Unicode escape sequences of the form \uxxxx
where the x
s are hexadecimal digits. However, these are different from ordinary string and character escapes in that you can use them anywhere in a Java program ... not just in string and character literals; see JLS sections 3.1, 3.2 and 3.3 for a details on the use of Unicode in Java source code.
See also:
-
The Oracle Java Tutorial: Numbers and Strings - Characters
-
In Java, is there a way to write a string literal without having to escape quotes? (Answer: No)
Solution 2:
char ch='"';
System.out.println(ch + "String" + ch);
Or
System.out.println('"' + "ASHISH" + '"');
Solution 3:
Escape double-quotes in your string: "\"Hello\""
More on the topic (check 'Escape Sequences' part)
Solution 4:
You can do it using a unicode character also
System.out.print('\u0022' + "Hello" + '\u0022');