This blog will describe the method of printing quotes in Java.
How to Print Quotes in Java?
In Java, double quotes are typically used to denote strings. However, only the value inside the double quotes is printed, not the double quotes themselves. So, how can we print quotes in Java?
To print quotes in Java, you can use:
- Escape Sequence character
- char
- Unicode Characters
Let’s check out each of the mentioned methods one by one!
Method 1: Printing Quotes in Java Using Escape Sequence Character
To print the quotes with the String, the most commonly used method is Escape Sequence or Escape Character. Java Escape Sequence contains letters, numbers, punctuation, and other symbols with backslash (\).
To print a single quotation by utilizing Escape Sequence Character (‘ ’), use ( \’), and for double quotation (“ ”) use (\”).
Example 1: Printing Double Quotes for a Whole String
In this example, we will print a String with quotation marks by using a double quote escape sequence (\”):
The output shows the added String is enclosed with double quotations:
Example 2: Print Double and Single Quotes Within a String
If you want to print a specific word with a single or double quote, only use an Escape Sequence with the selected word. As in this example, we will print the double quotes with the word “String”, and the single quote with “It’s”:
Output
Do not want to get into the hassle of using slashes? Look at the following method to learn about printing quotes in Java using char.
Method 2: Printing Quotes in Java Using char
You can also utilize “char” for printing quotes in a Java program. To do so, first, we will save the double quote (“) to a character and then use it with a string.
Example
In this example, we will create a char “q” that stores the double quotes:
Concatenate the created character with the specified words and store the resultant String in “s”:
Lastly, execute the String with having double quotes with the “System.out.println()” method:
Output
Method 3: Printing Quotes in Java Using Unicode Characters
The Unicode of the double quotes is “\u002”. It can also be added in a String to represent the double quotation.
Example
First, we have a String named “s”, which is enclosed in double quotes, a typical notation to define a String in Java:
Here, we will use Unicode “\u0022” in the print statement with the String “s” to print the string with double quotes:
Output
We compiled all the important information related to printing quotes in Java.
Conclusion
For printing quotes in Java, you can use three methods: Escape Sequence Characters, Char, and Unicode characters. To print a single quotation by utilizing Escape Sequence Character (‘ ’), use ( \’), and for double quotation (“ ”) use (\”), for char, firstly, save the double quote (“) to a character and then use it with a string. However, the Unicode “\u002” can also be added in a String to represent the double quotation. In this blog, we described the methods to print quotes in Java in detail.