Lecture 7: Strings and characters

Objectives

  • Distinguish between characters and strings
  • Use basic methods of the String class

Why are strings useful in computation?

  • Analysis of DNA/bioinformatics
  • Storing, searching, and analyzing documents written by humans
  • Other examples?

Characters

Another primitive type in Java is a character, which can be declared with char:

public class CharExamples {
    public static void main(String args[]) {
        char letter = 'c';
        char number = '1';
        char newline = '\n';
        char symbol = '@';
        char hmmm = (char) 0;
        char mystery = (char) 83;
       
       System.out.println(letter);
       System.out.println(newline);
       System.out.println(hmmm);
       System.out.println(number);
       System.out.println(mystery);
    }
}  

Let’s trace through the code together using the visualizer.

Note that a char stores a single character and uses single quotes ' as the delimiter.

How characters are stored in memory

You saw above it was possible to cast the integer 83 into a character, which ended up printing out S. Why?

It turns out that everything you store in memory will eventually be converted into an integer, and ultimately, binary, because of how the hardware of memory and the CPU works (recall, wires with current). For characters, there is a lookup called the ASCII table that allows you to see what integer values correspond to the human-readable characters we’re used to:

Strings

Now that we’ve seen characters, we can understand how a string is just an ordered sequence of characters:

String greeting = "Hello World!";

String letter = "h";
char character = 'h';

String empty = "";
String number1 = empty + 32 + letter; // makes the string "32h" 
String number2 = empty + 32 + character; // makes the string "32h" 
String updated = greeting + " How are you?"; // makes the string "Hello World! How are you?"

While it may appear that String is a built-in type in Java (like int or boolean), it’s not. A String is actually a class in Java, meaning it’s a user-defined type that (someone, at some point) specified. We’ll cover classes very soon, but for today, this means two things: 1. Strings have an internal representation that we can’t access directly 2. Strings, because they are a class in Java, come with methods that we can call on strings.

How are strings represented in Java?

Strings have a special place in Java because they are so commonly used and useful. As such, there is a lot of syntactic sugar around their use, such as being able to declare them like String greeting = "Hello World!"; and using the + operator to concatenate different types to strings, as above. Other classes won’t let you do this.

Because all strings are of the type String in Java, they are objects, and therefore, stored as complex/reference types. However, we don’t know exactly how they are stored (unless we look it up, and their internal representation could change). What we do know, from a user perspective, is they behave like an array of characters and individual letters can be accessed using indices. However, a String is not a character array; the following code wouldn’t compile:

String greeting = "Hello World!";

char letter = greeting[1]; //does not compile
int size = greeting.length; // does not compile

char[] greetingArray = {'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!'}; 
char letter = greetingArray[1]; // letter would store 'e'
int size = greetingArray.length; // size would be 12

So how can we use strings if we can’t do basic things like get their length or access specific characters?

The String API

Because String is a class in Java, every string you create is an object that you can call String methods upon. These methods are in the String API, and were written by the folks who specified the String class itself many decades ago.

There are hundreds of methods in that API, but we’ll focus on just a few useful ones this semester:

  • length()
  • charAt(int index)
  • indexOf(char letter)
  • substring(int start, int end)

Let’s implement the code above using these methods of the String class, as well as illustrate some additional functionality:

public class StringExamples {
    public static void main(String args[]) {
        String state = "Mississippi";

        int length = state.length();
        char secondLetter = state.charAt(1);
        for(int i = 0; i < state.length(); i++)
            System.out.println(state.charAt(i));
        
        char lastLetter = state.charAt(state.length() - 1);
        int index = state.indexOf('s'); // why is it 2?

        String substring = state.substring(2, 7); // returns "ssiss"

    }
}

Let’s run the code together. Note that substring includes the start index but stops before the end index.

Next class

We’ll start HW8 to get more practice with strings.