How to print Double Quotes In Java

Hi coders! we all know to print a nonexecutable statement or simply a message on screen we use System.out.print statement then we put the message between these double quotes ” “. The message gets printed on the screen. But what if we want to prints those double-quotes too.
You must be thinking it is easy to put these ” ” into another double quote. But that is a wrong procedure because you will be using two pair of double quotes and the message will lie in between these which will show an error. the compiler will think that your statement ends at second quote. Let’s start learning how to print double quotes in Java.

System.out.print(“”  hello world “”  );   WRONG 

this line will show error.you can try yourself.

Hence to print double quotes along with our message we will use an escape character ( \ ) goes by the name of backslash character, before the inside double quote. This will tell the compiler to escape a character.

Print double quotes in Java

For example:-

System.out.print(” \”hello world \” “);    CORRECT

Output = “hello world”

Print backslash character in Java

Similarly, you can print backslash character [ \ ] too. Just add (  \ ) before the special character.

System.out.print(” \\hello world \\ “);

Output:

\hello world\

Hope you understand the article.

You may also read:

Leave a Reply

Your email address will not be published. Required fields are marked *