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) 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:

herong> javac CharacterLiteral.java

herong> 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.

Table of Contents

 About This Book

 JDK - Java Development Kit

 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

 Control Flow Statements

 Bits, Bytes, Bitwise and Shift Operations

 Managing Bit Strings in Byte Arrays

 Reference Data Types and Variables

 Enum Types and Enum Constants

 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

 Java Modules - Java Package Aggregation

 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

 Assert Statements and -ea" Option

 Annotation Statements and Declarations

 Java Related Terminologies

 Archived Tutorials

 References

 Full Version in PDF/EPUB