How to print quotation marks in Java [3 ways]

Learn different ways to print quotation marks in Java. Anything inside double quotes is considered as a string. For example:

String str = "JavaHungry";
System.out.println(str);

The above code will print the string JavaHungry without quotation marks. In this post, you will find out how to print the string "JavaHungry" including the quotation marks in Java.

Read Also: String Interview Questions in Java

There are 3 ways to achieve our goal:
1. Using escape sequence [Recommended]
2. Using char in Java
3. Using Unicode characters in Java

Let's dive deep into the topic:

1. Using Escape Sequence:

Escape sequence is the easiest way to print quotation marks in Java i.e adding a backlash with the character as shown below in the example:

public class QuotationMarkOne {
    public static void main(String args[]) {
      String givenString = "\"AliveisAwesome\"";
      System.out.println("Printing givenString with quotation mark " + givenString);
    }
}

Output:
Printing givenString with quotation mark "AliveisAwesome"

2. Using char in Java:

We know that char in Java is represented in single quotes. In the below example we have surrounded the double quotes with a single quotes for e.g '"'. We will surround the double quotes with single quotes at the start and end of the givenString to get the desired output. How to add a char to a String you can find it out here.

public class QuotationMarkTwo {
    public static void main(String args[]) {
      char doubleQuote = '"';    
      String givenString = doubleQuote + "AliveisAwesome" + doubleQuote;
      System.out.println("Printing givenString with quotation mark " + givenString);
    }
}

Output:
Printing givenString with quotation mark "AliveisAwesome"

3. Using Unicode in Java:

Whenever we want to print special characters or non English characters, then we use Unicode characters. Every Unicode denotes a character, for exmaple, double quote Unicode is '\u0022'. It is similar to what we did in the second point i.e print quotation marks using char in Java as shown below in the code:

public class QuotationMarkThree {
    public static void main(String args[]) {
      char unicodeDoubleQuote = '\u0022';    
      String givenString = unicodeDoubleQuote + "AliveisAwesome" + unicodeDoubleQuote;
      System.out.println("Printing givenString with quotation mark " + givenString);
    }
}

Output:
Printing givenString with quotation mark "AliveisAwesome"

That's all for today, please mention in the comments in case you have any questions related to how to print quotation marks in Java.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry