2.6. Strings

Strings in Java are objects of the String class that hold sequences of characters (a, b, c, $, etc). Remember that a class (or classification) in Java defines the data that all objects of the class have (the fields) and the behaviors, the things that objects know how to do (the methods).

You can declare a variable to be of type String.

Note

Class names in Java, like String, begin with a capital letter. All primitive types: int, double, and boolean, begin with a lowercase letter. This is one easy way to tell the difference between primitive types and class types.

Run the following code. What does it print?

The code above declares an object variable named greeting and sets the value of greeting to the Java keyword null to show that it doesn’t refer to any object yet. So System.out.println(greeting); will print null. Object variables refer to objects in memory. A reference is a way to find the actual object, like adding a contact to your phone lets you reach someone without knowing exactly where they are. The value of greeting is null since the string object has not been created yet.

../_images/greeting.png

Figure 1: Initial value for an object reference

In Java there are two ways to create an object of the String class. You can use the new keyword followed by a space and then the class constructor and then in parentheses you can include values used to initialize the fields of the object. This is the standard way to create a new object of a class in Java.

greeting = new String("Hello");

In Java you can also use just a string literal, which is a set of characters enclosed in double quotes ("), to create a String object.

greeting = "Hello";

In both cases an object of the String class will be created in memory and the value of the variable greeting will be set to an object reference, a way to find that object.

coding exercise Coding Exercise:

Here is an active code sample that creates two greeting strings: one using a string literal and the other using new and the String constructor. Change the code to add 2 new strings firstname and lastname using the two methods and print them out with the greetings.

Now that greeting refers to an actual object we can ask the object what class created it. Try the following. What does it print?

The code above will first print class java.lang.String since greeting was created by the String class. The full name for the String class is java.lang.String. The java.lang part is the package name. Every class in the Java language is in a package and the standard classes like String are in the java.lang package. Every object in Java contains a reference to the class that created it. Also, every class contains a reference to its parent class. Yes, a class can have a parent class, just as people have parents. But, in Java a class can only have one parent. A class can inherit object fields and methods from a parent class, just like you might inherit musical ability from a parent. The fourth line will print class java.lang.Object because the parent class (superclass) of the String class is the Object class. All classes in Java inherit from the Object class at some point in their ancestry.

../_images/stringObject.png

Figure 2: Object variable of type String with a reference to a String object which has a reference to the String class which has a reference to the Object class.

2.6.1. String Operators - Concatenation

Strings can be appended to each other to create a new string using the + or += operator . This is also called concatenation.

Note

Note that spaces are not added between strings automatically. If you want a space between two strings then add one using + ” ” +. If you forget to add spaces, you will get smushed output like “HiJose” instead of “Hi Jose”. And remember that variables are never put inside the quotes (“”) since this would print the variable name out letter by letter instead of its value.

You can even add other items to a string using the + operator. The other item will be converted to a string using the toString operator if it is an object and then appended to the current string. All objects inherit a toString method that returns a string representation of the object.

coding exercise Coding Exercise:

What do you think the following will print? Guess before you hit run.

Note

If you are appending a number to a string it will be converted to a string first before being appended.

Since the same operators are processed from left to right this will print 1243. First 4 will be turned into a string and appended to 12 and then 3 will be turned into a string and appended to 124.

coding exercise Coding Exercise:

If you want the addition to take place before the numbers are turned into a string what should you do? Try to modify the code above so that it adds 4 + 3 before appending the value to the string. Hint: you used this to do addition before multiplication in arithmetic expressions.

What if you wanted to print out a double quote ” character? Since the double quote ” is a special character with meaning in Java, you would use a backslash escape sequence to print out a real quote. And if you wanted to print out a backslash , you would have to backslash it too . Another useful backslashed character is n which will put in a newline. Here are the escape sequences that may be used in the AP course:

2.6.2. groupwork Programming Challenge : Mad Libs

Have you ever played MAD LIBS? In this game, you first choose a bunch of words without looking at the story and then those words are filled into the story to make it sound very wacky! Fill in the variables below with Strings for each word, and then run to see the wacky story.

Then, working in pairs, come up with another silly story that uses at least 5 new String variables. When you’re done, try another team’s mad libs code. Your teacher may ask you to create this program in a Java IDE like repl.it so that you can use input to read in the words (see input examples using the Scanner class).

2.6.3. Summary

  • Strings in Java are objects of the String class that hold sequences of characters.

  • String objects can be created by using string literals (String s = “hi”;) or by calling the String class constructor (String t = new String(“bye”);).

  • new is used to create a new object of a class.

  • null is used to indicate that an object reference doesn’t refer to any object yet.

  • String objects can be concatenated using the + or += operator, resulting in a new String object.

  • Primitive values can be concatenated with a String object. This causes implicit conversion of the values to String objects.

  • Escape sequences start with a and have a special meaning in Java. Escape sequences used in this course include “, \, and n.

You have attempted of activities on this page
Next Section - 2.7. String Methods