Objects, Strings &
Instance Methods

Meet your first object in Java.

Robot Factory

One machine can build many different robots.

Factory Floor

Build a Mini Robot

Use the same machine, but change the settings each time.

Shell Color
Front Badge
Finished Robots
Factory Log

Blueprint vs Instance

A class is the plan, and an object is one real example.

Class

Think of the machine plan. It tells the factory what kind of robot to build.

Object

One finished robot is a real instance built from that same plan.

Java Example

Class:String

One Instance

Object:"Hello"

Two Kinds of Variables

Some variables store values, and some store references.

VariableKinds.java
java

Primitive

int, double, and boolean store the value itself.

Reference

A reference variable stores how Java finds the object.

String

String is your first important reference type in AP CSA.

Visualizing Variables

Primitive values sit in the variable directly; reference variables point to the object.

Meet String

Now treat text in Java as an object.

MeetString.java
java

String Class

String is the class name for this kind of text object.

One Object

new String("Hello") creates one real string object.

Use Its Methods

You can call methods such as length() on it.

Creating Objects

Start with new, then compare the shorter form.

StringCreation.java
java

new

new tells Java to create a fresh object.

Constructor

A constructor helps Java create that object.

String Literals

"Hello" is the shorter form for the same kind of object.

Combining Strings

Use + to build a new string from smaller pieces.

StringCombine.java
java

Join Text

+ joins two strings into one new result.

Add Primitives

A primitive can join a string and become text.

New Result

Concatenation makes a new string, not a changed old one.

Concatenation Quiz

In the later questions, pay close attention to when text appears.

ConcatenationQuiz.java
java
Question 1 of 4

Calling Methods

Use the dot operator to call a method on one object.

StringMethods.java
java

Object First

A method call begins with one object such as word.

Use Dot

The dot connects that object to one method.

Method Name

After the dot, write the method name with parentheses.

Quick Reference

Common String methods at a glance.

Assume: String word = "coding"
int length()
Returns the number of characters.
word.length() -> 6
int indexOf(String str)
Returns the first index of str, or -1.
word.indexOf("d") -> 2
String substring(int from, int to)
Returns the part from from to to - 1.
word.substring(1, 4) -> "odi"
String substring(int from)
Returns everything from from to the end.
word.substring(2) -> "ding"
boolean equals(Object other)
Checks whether two strings have the same text.
word.equals("coding") -> true
int compareTo(String other)
Compares two strings in alphabetical order.
word.compareTo("dog") -> less than 0
String[] split(String del)
Splits the string around the delimiter.
word.split("i") -> {"cod", "ng"}

length()

Use length() when you need the number of characters.

LengthExample.java
java

What It Returns

An int with the string's character count.

This Example

"coding" has 6 characters.

Good For

Counting characters before you loop or slice.

String Indexes

Indexes start at 0 and stop at length minus 1.

StringIndexes.java
java

Starts at 0

The first character is at index 0.

Last Index

The last character is at length() - 1.

Index Error

Going past the range throws StringIndexOutOfBoundsException.

indexOf()

Use indexOf() to find where a piece of text first appears.

IndexOfExample.java
java

What It Returns

An int index, or -1 if not found.

This Example

The first "d" in "coding" is at index 2.

Not Found

If the text is missing, Java gives back -1.

substring(from, to)

Take the part from from up to to - 1.

SubstringRangeExample.java
java

Start Included

The character at from is included.

End Excluded

The character at to is not included.

This Example

substring(1, 4) gives "odi".

substring(from)

Take everything from one index to the end.

SubstringFromExample.java
java

One Start

You only choose where the new string begins.

Runs to End

Java keeps every character after that point.

This Example

substring(2) gives "ding".

One Character

Use substring(index, index + 1) to make a one-letter string.

OneCharacter.java
java

Pick an Index

Start at the one character you want to take.

Add One

End at index + 1 so only one character is included.

Same Type

The result is still a String, just with length 1.

equals()

Use equals() to check whether two strings have the same text.

EqualsExample.java
java

What It Returns

A boolean: either true or false.

This Example

Both strings say "coding", so Java returns true.

Use This

For strings, compare the text with equals().

compareTo()

Use compareTo() when you want alphabetical order.

CompareToExample.java
java

Less Than 0

The first string comes earlier alphabetically.

Equal to 0

The two strings are alphabetically the same.

Greater Than 0

The first string comes later alphabetically.

split()

Use split() to break one string into smaller pieces.

SplitExample.java
java

Delimiter

Java splits the string at each delimiter.

Array Result

The result is a String[] with several pieces.

This Example

Splitting on "i" gives "cod" and "ng".

For Later

We will study split() more later, so just keep the big idea for now.

String Quiz

For each code sample, decide what is printed.

StringQuiz.java
java
Question 1 of 6

Immutability

String methods usually return a new result.

ImmutableString.java
java

Still Old

word.substring(0, 3) alone does not replace the old string.

Save the New Result

Use word = word.substring(0, 3) to keep the new text.

Null References

A reference can also point to no object.

NullExample.java
java

Empty String

"" is a real string object, so its length is 0.

null

null means the variable is connected to no object.

Why It Fails

Java can call length() on "", but not on null.

String Behavior Quiz

Now decide what happens after the method call.

StringBehaviorQuiz.java
java
Question 1 of 5

Java Class Family

As a supplement, see how familiar classes fit into one larger family.

The Magic of toString()

One more supplement: String is built in, and objects can join strings too.

StringBehindScenes.java
java

java.lang

String is built in, so Java gives it to you automatically.

Object + String

When an object joins a string, Java turns that object into text with toString().

Course Summary

Key ideas from today

Class vs Object

A class is a blueprint. An object is one real instance.

Reference Variables

String uses a reference, and that reference can also be null.

String Methods

Call string methods with the object name, a dot, and the method name.

Immutable Strings

String methods return new results, so save the new string when you want to keep it.

01 / 27