1. Strings & Characters

1.1. Objectives

  • String and character basics

1.2. Time Goal

  • 15 minutes on this section

1.3. Key Terms

string
A string is an object of the String class which holds sequences of characters. The String class also defines methods that you can execute on a string object like getting its length or getting a substring (copy of part of the string). Notice that the String class starts with an uppercase letter. All class names in Java start with an uppercase letter.
substring
A new string that contains a copy of part of the original string.
index
A number that represents the position of a character in a string. The first character in a string is at index 0.
length
The number of characters in a string.
Unicode
An international standard for representing characters in most of the world’s languages.
empty string:
The string “”, which contains no characters and has a length of zero.

1.4. Exercises

Note

Strings provide a method named charAt. It returns a char, a data type that stores an individual character (as opposed to strings of them).

String fruit = "banana";
char letter = fruit.charAt(0);

The argument 0 means that we want the character at index 0. String indexes range from 0 to n−1, where n is the length of the string. So the character assigned to letter is b.

b a n a n a
0 1 2 3 4 5

Characters work like the other data types we have seen. You can compare them using relational operators:

if (letter == 'a') {
   System.out.println('?');
}

Character literals, like ‘a’, appear in single quotes. Unlike string literals, which appear in double quotes, character literals can only contain a single character. Escape sequences, like ‘\t’, are legal because they represent a single character.

The increment and decrement operators also work with characters. So this loop displays the letters of the alphabet:

System.out.print("Roman alphabet: ");
char c = 'A';

while ( c <= 'Z') {
   System.out.print(c);
   c += 1;
}
System.out.println();

Java uses Unicode to represent characters, so strings can store text in other alphabets like Cyrillic and Greek, and non-alphabetic languages like Chinese. You can read more about it at http://unicode.org/.

In Unicode, each character is represented by a code point, which you can think of as an integer. The code points for uppercase Greek letters run from 913 to 937, so we can display the Greek alphabet like this:

System.out.print("Greek alphabet: ");
int i = 913;

while ( i <= 937) {
   System.out.print((char) i);
   i += 1;
}
System.out.println();

This example uses a type cast to convert each integer (in the range) to the corresponding character. Try running the code and see what happens.

The index of the character 'e' in the String "Apple" is

The value of the expression "my string".charAt(6) is

The value of the expression 'a' + 2 == 'c'

The output of the following code is

int i = 3;
char a = 'x';
System.out.println((char) (a - i));

Q-123: Q-122:

Answer the above question and submit before looking at the answer. You are given credit for providing a reasonable answer even if incorrect.

Why is the cast to char, i.e. the (char), necessary in the above code if we want the output to be a character?

The character stored in a is treated as an integer value. The value of 'x' is 120. When evaluating the expression a - i, it evalutes to 120 - 3 or 117. Since we are interested in the character value, the 117 needs to be converted (or cast) back to a char data type. The below shows the evaulation with out the cast.

    Drag the definition from the left and drop it on the correct concept on the right. Click the "Check Me" button to see if you are correct Review the summaries above.
  • the position of a character in a string
  • index
  • a new string with 0 to all characters copied from another string
  • substring
  • doesn't change
  • immutable
  • the number of characters in a string
  • length
Next Section - 2. String Equality