KotlinCS 124 LogoJava


String greeting = "Hello, world!";
String[] parts = greeting.split(" ");

Let’s 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 this is our first taste of what is yet to come.

Working with Text
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:

String welcome = "Hello, world!";

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

String remember = "You are not alone"; // Double quotes
char first = 'U'; // Single quotes
String fancy = "And if you need a \", we precede it with a backslash.";

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

// I translated this automatically, so it may say something stupid.
// If you know how to translate this correctly, get in touch!
String remember = "";

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.)

String remember = "➡️👤 are ❌️ alone";


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:

String first = "Xyz";
String last = "Challen";
System.out.println(first + " " + last + " is a cat");
String fullName = first + " " + last;
System.out.println(fullName + " is not a dog");

Throughout our examples, when we need to construct Strings including variable values, we typically use the + operator to merge variable values with surrounding Strings, as shown above. However, there is another option, which is to use the String.format method. Here’s an example:

String first = "Xyz";
String last = "Challen";
System.out.println(String.format("%s %s is a cat", first, last));
String fullName = String.format("%s %s", first, last);
System.out.println(String.format("%s is not a dog", fullName));

Let’s take a quick look at String.format, since it’s a bit of an unusual function compared to the ones we’ve seen previously.

String first = "Xyz";
System.out.println(String.format("%s is a cat", first, last));

You are welcome to use either the plus operator (+) or String.format when creating Strings that merge text literals and variable values—whatever works best for you!

System.out.println v. System.out.print
System.out.println v. System.out.print

As a final note, let’s examine the difference between System.out.println and System.out.print:

System.out.println("Hello, world!");

Practice: Reformat a Phone Number

Created By: CS 124 Staff
/ Version: 2020.9.0

Write a function called reformatPhoneNumber. It should take a String containing a phone number in the format 111-222-3333 and return it reformatted as (111) 222-3333.

You will want to explore the various String methods to help you with this task. In particular, you may find split and substring helpful. There are solutions that use split, others that use substring, and probably others that use neither!

Strings as Objects
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:

String example = "I'm an example";

Strings are Objects
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!


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 with this lesson, we may expect you to use some of their built-in features, and point you at the relevant documentation.

Dot Notation
Dot Notation

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

String name = "Chuchu Challen";

String Equality
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!

String first = "test";
String second = "test";
System.out.println(first == second); // This works, but it's incorrect!
System.out.println(first.equals(second)); // This is the right way to compare Strings

Creating Strings and new
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:

// "Here" is a String literal on the right side of the assignment
String example = "Here";

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

String example = new String("Here");

Wait, where did we see new before?

String example = new String("Where did I see new before?");

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:

String first = new String("test");
String second = new String("test");
System.out.println(first == second); // This no longer works!
System.out.println(first.equals(second)); // This is the right way to compare Strings

Practice: Email to NetID

Created By: CS 124 Staff
/ Version: 2021.8.0

Write a method called emailToNetID. It should take a String containing an @illinois.edu email like hello@illinois.edu and return the NetID, which in this case would be "hello", as a String. You can assume that the passed String will contain a single @ character. And you should assert that the passed String ends with @illinois.edu.

You will want to explore the various String methods to help you with this task, particularly split.

Homework: String to Latitude

Created By: CS 124 Staff
/ Version: 2022.8.0

It's common when working with data to have to convert it from one format to another. Frequently we find data as text and need to extract it to other formats so that we can work with it in our programs.

As an example, consider the (very special) position "40.10986682, -88.22831928", represented as a string containing latitude and longitude values separated by a comma with latitude first. Write a method stringToLatitude that takes a String in the format above as input and returns the latitude that it contains as a double. So for the string above you would return the double 40.10986682.

The two values will always be separated by a comma, but there may be a variable amount of whitespace surrounding each value—which is common in data that was entered by hand.

You should explore String methods that may help you, including split and trim. Once you have a String containing a double value, you can convert it to the double type using Double.parseDouble(string).

More Practice

Need more practice? Head over to the practice page.