Character Data

Character data is stuff like car licence plates, phone numbers and social security numbers, data that might consist of numbers but that you don't normally perform math on. The two main ways of storing character data is via the char primitive or the String class. The String class is generally preferable as it has a whole swag of methods for slicing, dicing and generally manipulating the characters. The char primitive data type is actually a 16bit integer that can store characters from the Unicode character set. You generally don't need to know much about Unicode unless you are involved in internationalisation (apologies if that is horribly English centred, but I am horribly English centred). Unicode is a way of storing almost any of the worlds character sets, so you can represent the accents in European languages or even Vietnamese, Chinese and Japanese that do not even use the European style character sets.

An important difference between the String class and the char data type is that when you put a character into a char variable you use single quotes and when you put a character into a String you use double quote.

Thus

String s = new String("a");
char a = 'a';

Note that a char only stores one character, do not try to store a string thus

char a='error';

Will not compile.

Last modified: Sunday, 20 September 2015, 07:21 PM