Java Strings

Before we get into Java Strings, what they are, and how they work, let’s talk about this series of posts.

This series is intended for those new to programming in Java, specifically those taking the Computer Science AP-A course. We’re going to start with the basics, covering what Strings are and working our way through the AP Java Subset section on strings. Towards the end we’ll look at a few other methods that you’re not expected to know as part of the AP-A exam but might come in handy.

We’re limiting a bit because Java Strings have a bunch of methods. For first year programmers it’s just not realistic that you would be able to master all of them.

Along the way you’ll be able to check your understanding by typing some code. If you’re interested in how the embedded code editors work, there’s a page explaining it.

If you have any questions along the way feel free to drop a comment on the bottom of each page.

What is a String?

Think of a string as a group of characters. Anything you might say or write.

When we’re writing Java code we use double quotes to tell Java that something should be a string like this.

System.out.println( "Hello" );

Notice that Hellois in double quotes. That tells Java that Hellois a string. Unlike some languages, Java differentiates between double and single quotes, so be sure that you’re using doubles.

Let’s take our first coding break. Extending off of the little snippet above, we want to take the following code and modify it so that it prints out the following.

Hello
There
Joe
Smith

Before you start, a couple of things to look at.

Java is a pretty code heavy language. You can’t just start typing code. The first two lines, the two that start with the word public, are defining a class and a method within that class. For now, let’s just look at the lines that start with System.out.println. The first line is done for you. Your job is to change the second, third and fourth lines so that they output the lines listed above.

Once you think you have a working solution, click on the test button. Your code will bounce off of a couple of servers to check that it’s getting the correct output.

Solution

If you’re struggling, here’s a working solution for this problem. We’ll put solutions near each coding problem in this series, hidden by the spoiler dropdown if you need a bit of help. In most cases it won’t the the only solution, but will be a working solution.

public class Example {
public static void main( String[] args ) {
System.out.println("Hello");
System.out.println("There");
System.out.println("Joe");
System.out.println("Smith");
}
}

String Indexes

One of the more confusing parts of working with strings for new coders is how the characters are positioned in the string. Instead of the first character being in position 1, it’s actually in position 0. This isn’t just a weird Java quirk. A lot of languages start strings with position 0.

So let’s look at Helloagain. There are 5 characters, but they’re in position 0-4. His in position 0 and ois in position 4.

 0 1 2 3 4 H e l l o

This is an odd concept if you’re just starting out in programming, but it’s important. It’s something we’ll come back to with some of the string methods.

What’s Next?

Now that we’ve talked a little about what Strings are, our next step is to look at how to store strings in variables. And we’ll do that in the next post in this series.

In this series

A quick, short introduction to strings in Java.
You can't always work with string literals. You need variables.
Concatenation is the fancy word for combining multiple strings.
Built in method to see if two strings are the same.
Need to know how many characters are in a string? There's a method for that.
indexOf / lastIndexOf