loading

Special Characters

Java Special Characters

Strings – Special Characters

Java will interpret this string incorrectly and produce an error since strings need to be placed inside quotes:

				
					String txt = "We are the so-called "Vikings" from the north.";
				
			

The backslash escape character is the way to get around this issue.

Special characters become string characters when you use the backslash (\) escape character:

Escape character Result Description
\' ' Single quote
\" " Double quote
\\ \ Backslash

A double quote is inserted into a string using the sequence \” :

Example

				
					String txt = "We are the so-called \"Vikings\" from the north.";
				
			

A single quote is inserted into a string using the sequence \’ :

Example

				
					String txt = "It\'s alright.";
				
			

A single backslash is inserted into a string via the sequence \\ :

Example

				
					String txt = "The character \\ is called backslash.";
				
			

Other typical escape sequences that work in Java include:

Code Result Try it
\n New Line Try it »
\r Carriage Return Try it »
\t Tab Try it »
\b Backspace Try it »
\f Form Feed
Share this Doc

Special Characters

Or copy link

Explore Topic