CIS 35A: Introduction to Java Programming

Home | Green Sheet | Lectures | Assignments | FAQ

Java

Java Programming
String variables
Special characters

Within a string, you can use escape sequences to include certain types of special characters.

Common escape sequences

Sequence Character
\n New line
\t Tab
\r Return
\" Quotation mark
\\ Backslash

Example 1: New line

String
"Code: JSPS\nPrice: $49.50"
Result
Code: JSPS
Price: $49.50

Example 2: Tabs and returns

String
"Joe\tSmith\rKate\tLewis"
Result
Joe Smith
Kate Lewis

Example 3: Quotation marks

String
"Type \"x\" to exit"
Result
Type "x" to exit

Example 4: Backslash

String
"C:\\java\\files"
Result
C:\java\files
Previous | String variables | Create a String object | Join and append strings | Special characters | Next