Java char type

In this chapter you will learn:

  1. How to declare Java char type
  2. How to create char Literals
  3. How to use escape sequences and create special characters
  4. Store unicode into a char
  5. How to create a Character object from char value

char type

In Java, char stores characters. Java uses Unicode to represent characters. Unicode can represent all of the characters found in all human languages. Java char is a 16-bit type. The range of a char is 0 to 65,536. There are no negative chars. Here is a program that demonstrates char variables:

public class Main {
  public static void main(String args[]) {
    char ch1, ch2;
//from ja v a 2s .  c o m
    ch1 = 88; // code for X

    ch2 = 'Y';

    System.out.print("ch1 and ch2: ");
    System.out.println(ch1 + " " + ch2);//ch1 and ch2: X Y
  }
}

The code above generates the following result.

ch1 is assigned the value 88, which is the ASCII (and Unicode) value that corresponds to the letter X.

char type value can be used as an integer type and you can perform arithmetic operations.

public class Main {
  public static void main(String args[]) {
    char ch1;//from j  a  v  a 2 s .  c  o m

    ch1 = 'X';
    System.out.println("ch1 contains " + ch1);//ch1 contains X 

    ch1 = (char)(ch1 + 1); // increment ch1
    System.out.println("ch1 is now " + ch1);//ch1 is now Y
  }
}

char Literals

Characters in Java are indices into the Unicode character set. character is represented inside a pair of single quotes. For example, 'a', 'z', and '@'.

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

  }
}

Another example to define char type value and assign sign characters to them:

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

    System.out.println("ch is " + ch);//ch is #
    ch = '$';

    System.out.println("ch is " + ch);//ch is $
    ch = '%';

    System.out.println("ch is " + ch);//ch is %

  }
}

Escape char value

The escape sequences are used to enter impossible-to-enter-directly characters. '\'' is for the single-quote character. '\n' for the newline character.

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.

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

  }
}

Character is a simple wrapper around a char.

Store unicode into a char

The following code stores unicode value into a char variable. The unicode literal uses \uxxxx format.

public class Main {
  public static void main(String[] args) {
    int x = 75;/*j a  v a  2s  .  c o m*/
    char y = (char) x;
    char half = '\u00AB';
    System.out.println("y is " + y + " and half is " + half);

  }
}

Create a character object from char value

Character(char value) creates a Character object for char value.

public class Main{
  public static void main(String[] argv){
    System.out.println(new Character('a'));    
  }
}

The output:

Next chapter...

What you will learn in the next chapter:

  1. How to compare two characters
Home » Java Tutorial » Primitive Data Types

Introduction

    Java Primitive Data Types

Boolean

    Java boolean type
    Java boolean type conversion
    Convert string value to boolean
    Convert boolean to string

Char

    Java char type
    Compare two char values
    Change char case
    Java char conversion
    Java char value and its attributes

Byte

    Java byte type
    Convert Byte to String
    Convert String to byte
    Byte object constructor
    Byte's max and min value
    Compare two byte values
    Convert Byte to byte, double, float, int, long and short

Short

    Java short type
    Short min/max value and size
    Create Short object
    Compare short values
    Convert short to String
    Convert Short to primitive types
    Convert string to short
    Reverse bytes

Integer

    Java int type
    int max/min value
    Create Java integer
    Convert int to binary, hexadecimal and octal format
    Compare integer values
    Integer sign
    Convert string to int
    Convert int to primitive types
    Convert int to String
    int bit operations

Long

    Java long type
    Compare two long values
    Convert long to binary, hex and octal
    Convert long value to primitive types
    Convert String to long value
    Convert long to String

Float

    Java float type
    Java float type conversion
    Predefined value for float type
    Compare two float value

Double

    Java double type
    Deal with NaN double value
    Compare two double values
    Java double type creation and comparison
    Java double type conversion

Data Type Conversion

    Java Automatic Type Conversion and Casting
    Data type casting
    Java type promotion
    Autoboxing and auto-unboxing