Strings and Characters


Character Variables

The char data type is one of the primitive or atomic data types in Java. It is designed to hold a single character. Some programmers pronounce this data type like the word that means to burn something but most programmers prefer to pronounce it like the first syllable of the word character for which it is actually the abbreviation.

Declaring a character variable

In code character variables are defined using the keyword char followed by the variable name. Names for char variables follow the standard Java rules for variable names. For example:
	char ch;	
	char letter;
	char number;	// Don't let the name fool you, it's still a char
	char acter;		

Storing a value a character variable

Storing a value in a character variable is simple. It consists of the variable name, an equals sign, and a single character in single quotes. For example:
	ch = 'a';      // Store the letter 'a' in char variable ch	
	letter = 'Z';  // Store the letter 'Z' in char variable letter   
	number = '2';  // Store the letter '2' (not the number 2) in char variable number
	acter = 'b';   // Store the letter 'b' in char variable acter		

What characters can be stored in a char variable?

Any of the ASCII characters can be stored in a char variable. By the way, ASCII stands for American Standard Code for Information Interchange. The printable ASCII characters are shown in figure 1.


Figure 1: The ASCII character set

String Variables

A String in Java is actually a class in the Java Class Library. It can be used to hold just about any line of text.

Declaring a String variable

In code String reference variables are defined using the class name String followed by the variable name. Names for String variables follow the standard Java rules for variable names. For example:
	String s;
	String str;
	String myName;	

Creating instances of Strings and storing values in them

Instances of Strings can be created by using the new operator or just by setting a String variable to some text in double quotes. This actually instantiates an instance of the String class and then stores the string in it. For example:
	s = new String();   // Instantiate a new String
	s = "My string";    // Store "My string" in the String instance s
	// Both lines below first instantiate a new String object and then
	//   store the shown text in it.  In the first an instance of String
	//   is explicitly created using the new operator. In the second the
	//   instance of String is automatically instantiated just by using
	//   the equals to store a string in it.
	str = new String("This is a String."); 
	myName = "Able Programmer";

String Functions

Because the String class is part of the Java Library there are quite a few built-in functions you can use with any instance of String that you create in a program. A few of these are listed below. To see all of the functions click here to go to the Java Library web page then scroll down in the list on the lower left and select String. In the examples below the String myName has "Able Programmer" stored in it.