Kotlin
Java

Strings
Java

Created By: Geoffrey Challen
/ Updated: 2021-08-30

Today we take two big steps forward in our journey in computer science. First, we'll learn how to work with text data in Java, using a data type called a String. Strings also represent another step forward, since they are our first example of a Java object. We'll spend a lot of time discussing Java objects in future lessons, so today is our first taste of what is yet to come.

Working with Text

Language is one of the things that makes humans special. And, while many animals communicate through vocalizations, written language is even more unique to our species. Words and text have long played an incredibly important role in human societies. So clearly this is a kind of data that we want to be able to work with in our computer programs.

Happily, Java has a special data type specifically for working with text:

Note how Strings differ from chars, in that they are enclosed in double quotes (") rather than single quotes ('):

Java Strings are not limited to the limited number of characters that we can store in a char:

A full discussion of Unicode and how characters are represented in modern programming languages is outside the scope of this class. But it's a fascinating story with lots of interesting wrinkles. Safe to say, we have fully overcome the limitations of early programs ability to work with non-latin alphabets. Unicode even includes emoji:

(Note that the in-browser editor gets a bit weird around emoji, probably because they aren't the same width as other characters.)

Concatenation

One useful thing that we can do with Strings is combine them. Java allows us to do this using the + operator, which is the only way that a mathematical operator is reused in Java:

Strings as Objects

On one hand, Strings just seem like any other Java variable. But there is something new going on here. Let's explore together:

Identify some of the interesting features of Strings.

Strings are Objects

The unusual behavior that we observed above is due to the fact that String is not one of the eight primitive types. Everything else in Java is an object. One way to distinguish between primitive and object types is, by convention, all object types are capitalized.

We'll be talking a lot about objects in future lessons, but for now we'll define an object as something that combines state and behavior, or data and functionality. Java objects can be seen as uniting two of the basic building blocks that we've already been exploring: variables and methods. Like a variable, Java objects store information. Strings store a series of characters. But in addition, Java objects also come with built-in methods that we can call! Frequently, those methods operate on the data contained in the object.

Let's look at how that works out with Strings, our first object. Note that this is a screencast, rather than a walkthrough, so that we can consult some documentation together!

Go through the different methods that you can call on Strings and browse the Javadoc documentation.

The best way to familiarize yourself with these features is to browse the official String documentation. Over the set of homework problems on Strings that start today, we may expect you to use some of their built-in features, and point you at the relevant documentation.

Dot Notation

To call a method on a String, we use so-called dot notation. Let's explore that in the following walkthrough:

Discuss dot notation and using it to call String methods.

String Equality

Because Java Strings are objects, we need to compare them with each other in a new way: using the .equals method.

This can be a bit confusing, because sometimes our old friend the == equality operator will still work on Strings!

Creating Strings and new

In Java, Strings are one of only two objects that support literals. We've been using that support throughout this lesson:

But there is another way to create Strings that hints more at their true nature as Java objects:

Wait, where did we see new before?

Look back at Java arrays where we also used new.

Note that there is a small and fairly unimportant difference between initializing a String using a literal or with new. For our purposes it is never important, and so we'll typically use a literal.

However, when we create Strings using new, we can demonstrate that the == equality operator does not work for Strings:

Show how to complete the homework problem above. Feel free to cover multiple approaches!

Solution Walkthrough

Show how to complete the homework problem above. Feel free to cover multiple approaches!

Solution Walkthrough