University of Toronto - Fall 2000
Department of Computer Science

Week 3 - Strings

Declaring and creating a String object

String class

Before we go on to write the other methods in our Book class, let's take a look at the String class. Its name starts in uppercase, because that's the convention for class names.

We've seen strings in our output statements, but they also have several useful methods that you can use to modify strings and create new ones. We'll look at the String class both as a source of method examples and because it's a very important class.

Strings are not exactly part of Java, but they're not exactly not part of it, either. Strings are defined in a library, not in the language itself … but the library is always provided, and you couldn't write a useful program without strings anyway.

Declaration and instantiation of a String

  String s = "hi";
Note, that this is the same as:
  String s = new String("hi");

Memory Model

What happens in memory?