Primitive Data Types

Char

🙋 Need help? Ask an expert now!

The next datatype we will look at is character, which is represented with char in Java. A char can almost represent any character in any language. To see a full list of potential characters, look up the Unicode character table. View the code below for an example of how to declare and use the character datatype.

CharDataTypeExample.java
package exlcode;

public class CharDataTypeExample {

  // char can be any one character, including spaces
  public static char exampleVariableOne = 'c';
  public static char exampleVariableTwo = 'V';
  public static char exampleVariableThree = ' ';

  public static void main(String[] args) {
    System.out.println(exampleVariableOne);
    System.out.println(exampleVariableTwo);
    System.out.println(exampleVariableThree + "Hello World!");
  }
}

char holds a single character with an apostrophe on each side. Notice that double quotation marks will not work for characters. Keep in mind that upper and lower case characters are represented by a different pattern. Additionally, special characters such as punctuation and spaces can be represented by the char data type as well.

Edit Me on GitHub!

Application Question

Which one of these is a valid character declaration?