Lesson 4 – Strings

Ok in this lesson let’s learn about Strings and the operations we can use on them.

Downloads

To get started you can use the basic starter project and follow the steps to compile the code examples for the lesson.

Setup

  1. Create a new package called: com.codenerve.beginner.lesson4
  2. Create a new class file called: Strings.java

If you need help with this, please go back and look at lesson 2

Creating a String

First, let’s create a new local variable inside a main method from which this program will run.

Remember a field is created inside the class declaration but before any methods. A local variable is created inside a method (the main method in our case).

ClassFields

So add the following to the main method:

As you can see we have created something new called a String. A String is not a primitive type; it is actually another java class file which represents a String. This class is part of the JDK(See lesson 0 – setting up if you can’t remember what this is).

In short, a String is a collection of chars. A primitive type we learnt about in lesson 2. These means it can have almost any number of values including a letter, a word, a number, a sentence or a combination of all of them!

Continue to copy the code snippets into the main method as you work through this lesson. Run the program at each stage and observe the output of the System.out.Println statements produce.

Comparing

The equals operator == will compare two strings and return a boolean (true or false) if the strings match.

There are also some Methods in the String class which we can use to manipulate our strings:

We’ll learn more about Methods and how to create our own in the next lesson.

String concatenation

Concatenation in Java is the ability to join strings together into a new String:

Conversions

We also have the ability to convert our Strings to other data types:

It’s worth knowing that these examples are not exhaustive. There are many more Methods on the String class you can use for comparing, modifying and converting your Strings. You should also notice in the last example of the previous code snippet; we used another Class called Integer. At this stage, I encourage you to play about with these. See what happens when you try to convert a String = “a” into an int for example(will it break the program?).

TIP: Autocompletion in IDE’s (Including Eclipse)

Something you’ll use a lot when writing code is auto-completion. In eclipse, this is called IntelliSense. Let’s go back and look at one of the first examples:

When typing this out, after the “hello” hit CTRL + Space.

When you do this you should see:

intellisense

From this list, you can see all the available methods on the String class, and you can select the “equals” method and complete the example. I can’t stress enough how useful this will be. Try it after other dots in the samples or in the middle of variables.

Another example is to create the main method you’ve been using:

Screen Shot 2014-02-19 at 21.52.31

In Eclipse, hitting enter with “main – main method” highlighted will create:

In IntelliJ, this can be done using ‘psvm’ and tab.

Handy! This shortcut will save time and help you become more familiar with the available Methods and classes from the JDK.

Ok, Next up we’ll be creating our methods.

Next: Methods

Menu