PDF download Download Article
Easy-to-follow steps on using double quotes in Java when " doesn't work
PDF download Download Article

You've discovered that the double quote symbol " will not work inside a Java print instruction. You'll need to find an alternate way to tell the compiler to print this symbol, instead of interpreting it as instructions to close the string. The escape character is the most straightforward way to do this. While there's no need to learn the ASCII code in this case, it's another handy option to know for symbols that do not have an escape sequence.

Things You Should Know

  • The easiest way to signal a quotation mark in Java is to use the backslash key.
  • Surround the text you want to print with quotation marks in backslashes.
  • Alternatively, use "char(34)" to represent quotation marks.
Method 1
Method 1 of 2:

Using an Escape Character

PDF download Download Article
  1. As you know, the double quote symbol " has a special meaning in Java (displaying text). Whenever you want to ignore one of these meanings, use the escape character \ (backslash). This character tells the compiler that the next character is part of an alternate instruction.
    • Make sure you are hitting the backslash key, not the forward slash. The backslash key is next to the } key on most English keyboards.
  2. These two characters together are called an escape sequence. Each escape sequence has a special meaning. In this case, \" just means "insert a double quote symbol here,", without interpreting it as the beginning or end of text.
    • You will need to use this sequence for each individual double quote you want to display.
    Advertisement
  3. The escape sequence does not affect the rest of your code. There is no need to type anything else to return to normal programming.
  4. One common mistake is to leave out the plain old " mark in your program. Remember that \" is just for display, and does not remove the need to encase your display text in quotation marks. Here's an example:
    • 1. The string for displaying "Hello" is \"Hello\"
    • 2. To instruct the compiler to print this text, we wrap it in quotes: "\"Hello\"".
    • 3. Here's what this looks like in a complete line of code:
      System.out.println("\"Hello\"");
      
  5. Advertisement
Method 2
Method 2 of 2:

Using ASCII Code

PDF download Download Article
  1. Java can easily represent ASCII symbols using the char type. 34 is the ASCII code for the " symbol, so write char(34) to display " without using its special meaning.
    • You can look up a symbol's ASCII code by searching for an ASCII code table online.
  2. If you make the mistake of putting this code inside the string, your program will print it exactly as it appears in your program: char(34). Here's the proper method of displaying "Hello" (with the quotation marks) using this method:
      System.out.println((char)34+"Hello"+(char)34);
      
  3. Advertisement

Community Q&A

Search
Add New Question
  • Question
    I put System.out.println(char)34 + (char)40 + "ACE" + (char)41 + (char)34); but output is 74ACE"). How do I fix it?
    Community Answer
    Community Answer
    You haven't put an apostrophe (') before the Char value. It is compulsory to keep your character constant in apostrophes, e.g. char ch = '34';.
  • Question
    How do I print the % symbol after any decimal value in Java?
    Pingu
    Pingu
    Top Answerer
    You need to use the printf() function. Since the % is used as a format sign, you need to escape it to literally write %, which is done by writing %%. For example, if you have your decimal value stored in a variable decValue, you can write: printf("%f %%\n", decValue); .
  • Question
    How do I print \ in Java?
    Community Answer
    Community Answer
    The backslash character is used in Java and many other programming languages as an escape character. If you'd like to use the backslash character as normal, escape it properly ("\\").
Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit
Advertisement

Video

Tips

  • Here's a list of other Escape Sequences in Java:
    • \t - Insert a tab in the text at this point.
    • \b - Insert a backspace in the text at this point.
    • \n - Insert a newline in the text at this point.
    • \r - Insert a carriage return in the text at this point.
    • \f - Insert a formfeed in the text at this point.
    • \' - Insert a single quote character in the text at this point.
    • \" - Insert a double quote character in the text at this point.
    • \\ - Insert a backslash character in the text at this point.



Submit a Tip
All tip submissions are carefully reviewed before being published
Name
Please provide your name and last initial
Thanks for submitting a tip for review!
Advertisement

You Might Also Like

Compile & Run Java Program Using Command PromptA Beginner’s Guide to Compiling & Running Java Programs
Check Your Java Version in the Windows Command LineUse Easy Windows CMD Commands to Check Your Java Version
Do Division in Java Do Division in Java (Integer and Floating Point)
Set Java Home Set JAVA_HOME for JDK & JRE: A Step-by-Step Guide
Compile and Run Java Program by Notepad Compile and Run Java Programs Using Notepad++
Check Java Version on a Mac Quickly and Easily Check Java Version on a Mac
Check Null in JavaCheck Null in Java
Create an Executable File from EclipseCreate an Executable File from Eclipse
Add JARs to Project Build Paths in Eclipse (Java)Add JARs to Project Build Paths in Eclipse (Java)
Update JavaUpdate Java
Calculate Percentage in JavaCalculate Percentage in Java
Program in JavaProgram in Java
Call a Method in JavaCall a Method in Java
Create JAR File Create a JAR File With Eclipse
Advertisement

About This Article

Tested by:
wikiHow Technology Team
wikiHow is a “wiki,” similar to Wikipedia, which means that many of our articles are co-written by multiple authors. To create this article, 16 people, some anonymous, worked to edit and improve it over time. This article has been viewed 210,002 times.
How helpful is this?
Co-authors: 16
Updated: February 4, 2024
Views: 210,002
Categories: Java
Thanks to all authors for creating a page that has been read 210,002 times.

Is this article up to date?

Advertisement