UOB - String Objects (Lesson)
String Objects
Introduction
In java, everything that is not a primitive data type is an object. String is a data type that seems like primitive data but it is actually an object.
String
A String is made up of characters. Recall that characters are a form of primitive data. When we put them together to make up a String we get an object.
Strings can be created by using String literals. A String literal is literally what you see in between double quotation marks (“ “). When Java sees a String literal in double quotations it creates a String object.
String word = "yarn";
In the above example, the variable word refers to the String object. We previously learned how variables are used for primitive data types. They are also used for objects. The name of the variable lets us know where the object is located in memory. It does not mean the variable name is equal to the object.
Strings are a special type of object because we can treat them like primitive data. Remember that we usually would use the new keyword when creating any object. Strings are the exception. Since they are used so often and are similar to primitive data in their use, we can take a shortcut when creating them. The above example works the same as the following:
String word = new String("yarn");
Both statements do the same thing.
If we wanted to declare an object we use the class name and a variable.
String name;
To create an object we have to use the new operator, the class name, and sometimes parameters.
name = new String ("Mickey");
Remember this is called instantiation of an object. The variable, name, is an instance of the String class. The keyword, new, is an operator that calls a constructor which is a method that has the same name as the class. Constructors are used to create the object with any data that each instance of the object will contain. In this example, the constructor is passed a String literal that specifies what characters the String object will hold.
Once an object is created, we can use the dot operator to access any methods within the class. Because the String class is used so often, it can act as primitive data. That is why Strings can be created without using the new operator. String objects are immutable meaning that any “changes” to the String actually create a new String object.
Concatenation
Click below to learn more about concatenation.
String Indexes
Strings are very special objects because they contain indexes. The indexes label each character in a String.
Indexes always start at 0.
name = new String ("Mickey");
The index of M for String name is 0.
The index of y is 5.
This table shows the index for each character in the String.
0 | 1 | 2 | 3 | 4 | 5 |
M | i | c | k | e | y |
Escape Sequences
Click below to learn about Escape Sequences.
Click on "Runestone Academy" below to open the required reading that is listed.
Runestone Academy: AP CSA – Java Review
Links to an external site.:
READ: 4.1 – What is a String?
READ: 4.2 – Declaring and Creating Strings
Click on "Introduction to Computer Science Using Java" below to open the required reading that is listed.
Introduction to Computer Science Using Java
Links to an external site.
READ: Chapter 44: Strings and Object References
Complete the quiz for practice.
Parameters and Objects
Practice-It! Self-Check
- Go to the PracticeIt website Links to an external site..
- Create an account.
- Click on Start Practicing!
- Go to the most recent edition.
- Click on Chapter 3: Parameters and Objects.
- Complete Self-Check: 3.18
IMAGES CREATED BY GAVS