Double quotes are mainly used to indicate a string. When we print any string, the double quotes are not printed but only the value inside them is printed.
Methods:
Double quotes can be inserted using the following ways in java as listed below:
- Using Escape Sequence character
- Using char
- Using Unicode Characters
Let us discuss above listed ways in detail alongside implementing the same to get a fair understanding.
Method 1: Using Escape Sequence character
The first method to print the double quotes with the string uses an escape sequence, which is a backslash ( \ ) with a character. It is sometimes also called an escape character. Our goal is to insert double quotes at the starting and the ending point of our String. ‘\’ is the escape sequence that is used to insert a double quote.
Below we can see that we are using this escape sequence inside our string, and the output shows the string with quotations.
Example
Java
// Java Program to Print Quotation Marks in a String // Using Escape Sequence Character // Importing input output classes import java.io.*; // Main class to print quotes public class GFG { // Main driver method public static void main(String[] args) { // Custom input string String str = " \"Akshit Loves Lazyroar\" " ; // Print and display the above string on console System.out.println(str); } } |
"Akshit Loves Lazyroar"
Method 2: Print Double Quotes Using char in Java
We can also use char to print the double quotes with the string. First, we have to convert the double quote ( ” ) into a char.
In the below example, we have singleQuotesChar with a double quote surrounded by single quotes. The double-quote represents a string, and the single quote represents a char. Now, as our double quote has become a char, we can concatenate it with the string at both the starting and ending points.
Example
Java
// Java Program to Print Quotation Marks in a String // Using char // Importing input output classes import java.io.*; // Main class to print quotes public class PrintQuotes { // Main driver method public static void main(String[] args) { char value = '"' ; String str = value + "Akshit Loves Lazyroar" + value; System.out.println(str); } } |
"Akshit Loves Lazyroar"
Method 3: Using Unicode Characters
Whenever we want to print or use any character like symbols or non-English characters, we can use Unicode characters. Every Unicode represents a character, and \u0022 means a double quote.
We need to convert the Unicode to a char and then concatenate \u0022 with the string. The Unicode characters are universal characters encoding standards. It represents the way different characters can be represented in different documents like text files, web pages, etc. The Unicode supports 4 bytes for the characters. UTF-8 has become standard character encoding it supports 4 bytes for each character. There are other different Unicode encodings like UTF-16, UTF-8. Character literals in java are constant characters in java. They are expressed in single quotes ‘a’, ’A, ‘1’, ’!’, ‘π’, ‘$’, ’©’. The data type that can store char literals is char.
Example
Java
public class PrintQuotes { public static void main(String[] args) { String str = '\u0022' + "Akshit Loves Lazyroar" + '\u0022' ; System.out.println(str); } } |
"Akshit Loves Lazyroar"