Java char value escape

In this chapter you will learn:

  1. Why to escape char value
  2. Syntax to escape char value
  3. Example - char escape value
  4. List of Escape value

Description

The escape sequences are used to enter impossible-to-enter-directly characters.

Syntax

'\'' is for the single-quote character. '\n' is for the newline character.

Example

For octal notation, use the backslash followed by the three-digit number. For example, '\141' is the letter 'a'.

For hexadecimal, you enter a backslash-u (\u), then exactly four hexadecimal digits. For example, '\u0061' is the ISO-Latin-1 'a' because the top byte is zero. '\ua432' is a Japanese Katakana character.


public class Main {
  public static void main(String[] argv) {
    char ch = '\'';
/*from   w w w.j a va 2  s  .  c om*/
    System.out.println("ch is " + ch);//ch is '

  }
}

Character is a simple wrapper around a char.

The code above generates the following result.

Escape value list

The following table shows the character escape sequences.

Escape SequenceDescription
\dddOctal character (ddd)
\uxxxxHexadecimal Unicode character (xxxx)
\'Single quote
\"Double quote
\\Backslash
\rCarriage return
\nNew line
\fForm feed
\tTab
\bBackspace

Next chapter...

What you will learn in the next chapter:

  1. How to use Java byte type
  2. Size and value for Java byte type
  3. Example - Java byte type
Home »
  Java Tutorial »
    Java Langauge »
      Java Data Types
Java Primitive Data Types
Java boolean type
Java char type
Java char value escape
Java byte type
Java short type
Java int type
Java long type
Java float type
Java double type
Java String type
Java String Escape
Java String Concatenation