Java Escape Characters

Sep 2, 2025

Overview

This lecture explains how to print special characters in Java using escape characters, focusing on handling double quotes, backslashes, tabs, and newlines.

Printing Text in Java

  • Use System.out.println() to print text followed by a new line.
  • Use System.out.print() to print text without moving to a new line.
  • Any text in double quotes will be output to the screen as is.

Printing Special Characters

  • Most symbols (e.g., ! @ # $) can be printed directly like letters and numbers.
  • Printing double quotes within a string will cause an error due to ambiguity in string boundaries.

Escape Characters in Java

  • Use a backslash (\) as an escape character to "escape" normal rules for certain symbols.
  • To print a double quote, use \" inside the string.
  • To print a single quote, use \' (though usually needed in char literals).
  • To print a backslash, use \\ because a single backslash is reserved for escaping.

More Escape Sequences

  • Use \t for a horizontal tab in the output.
  • Use \n to create a new line within the printed text.
  • Spaces combined with \n will show up as leading spaces on new lines.

Summary of Usage

  • Escaping allows printing problematic characters like quotes and slashes.
  • Use \\ for a backslash, \" for double quotes, \t for tab, and \n for new line.
  • Remembering all escapes is less important; refer to notes as needed.

Key Terms & Definitions

  • Escape Character — A backslash (\) used to signal Java to treat the following character specially, overriding its normal function.
  • String Literal — Text enclosed in double quotes that Java interprets as a sequence of characters.

Action Items / Next Steps

  • Review and practice using escape sequences in your own Java print statements.
  • Check provided notes in the course description for reference.
  • Watch the next video in the Java beginner series for continued learning.