Literal Formats for Character - Example

This section provides a tutorial example on how to use character literals in different formats: single characters or escape sequences enclosed in single quotes.

Now let's see a tutorial program example on character literals in different formats:

/* CharacterLiteral.java
 - Copyright (c) 2014, HerongYang.com, All Rights Reserved.
 */

class CharacterLiteral {
   public static void main(String[] a) {
      java.io.PrintStream o = System.out;
      o.println("Character Literals:");
      o.println("   ASCII char:     'A'       = "+'A');
      o.println("   Control char:   '\\n'      = "+'\n');
      o.println("   Special char:   '\\\\'      = "+'\\');
      o.println("   Unicode char:   '\\u0041'  = "+'\u0041');
      o.println("   Decimal code:   '\\101'    = "+'\101');
   }
}

Compile this example program in a command window and run it:

C:\>\progra~1\java\jdk1.8.0\bin\javac CharacterLiteral.java

C:\>\progra~1\java\jdk1.8.0\bin\java CharacterLiteral

Character Literals:
   ASCII char:     'A'       = A
   Control char:   '\n'      =

   Special char:   '\\'      = \
   Unicode char:   '\u0041'  = A
   Decimal code:   '\101'    = A

Hope you know how to use character literals properly now.

Last update: 2014.

Table of Contents

 About This Book

 Installing JDK 1.8 on Windows

 Execution Process, Entry Point, Input and Output

Primitive Data Types and Literals

 Data Types Supported in Java

 Integer Data Types

 Floating-Point Data Types

 Logical (Boolean) Data Type

 Literals of Primitive Types

 Literal Formats for Integers

 Literal Formats for Integers - Example

 Literal Formats for Floating-Point

 Literal Formats for Floating-Point - Example

 Literal Formats for Characters

Literal Formats for Character - Example

 Bits, Bytes, Bitwise and Shift Operations

 Managing Bit Strings in Byte Arrays

 Reference Data Types and Variables

 StringBuffer - The String Buffer Class

 System Properties and Runtime Object Methods

 Generic Classes and Parameterized Types

 Generic Methods and Type Inference

 Lambda Expressions and Method References

 Execution Threads and Multi-Threading Java Programs

 ThreadGroup Class and "system" ThreadGroup Tree

 Synchronization Technique and Synchronized Code Blocks

 Deadlock Condition Example Programs

 Garbage Collection and the gc() Method

 Outdated Tutorials

 References

 PDF Printing Version