Java Char

char datatype

Unlike a String which, in Java, is an actual object with a full blown class, a char is a primitive datatype and represents a single character. While a String could have a single character, two characters or more, a char is limited to a single character. You use single quotes to designate a char as in the example below.

Examples of properly declared and initialized chars.

  • char myChar = 'a';
  • char myChar2 = '@';
  • char myChar3 ='1';

Examples of improper chars.

  • char myBadChar = "a"; You can't use double quotes(" ") with chars. In Java, only use double quotes with Strings .
  • char myOtherBadChar ='ab'; A char can store only 1 character, not two.
Site Map | Resources | Online Math Games | Fraction Games Online | TOS | Programming Homework Help Sitemap