Java

How to Print Quotes in Java

While programming in Java, text data is often stored and processed using Strings. However, Strings can also contain numeric information and special characters such as email addresses with multiple data types such as numbers, alphabets, and special characters. There are also situations when it is required to print the strings in quotations like “5 o’clock“, “b’day“, and others.

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 (\”):

System.out.println(""The string with quotation"");

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”:

System.out.println("It\'s the "String" with quotation");

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:

char q = '"';

Concatenate the created character with the specified words and store the resultant String in “s”:

String s = "The" + q + "string" + q + "with quotation";

Lastly, execute the String with having double quotes with the “System.out.println()” method:

System.out.println(s);

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:

String s= "The String with Quotation";

Here, we will use Unicode “\u0022” in the print statement with the String “s” to print the string with double quotes:

System.out.println('\u0022' + s + '\u0022');

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.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.