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

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

Object

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

Java Example

The class is String.

One Instance

One object is "Hello".

🗃️ Two Kinds of Variables

Some variables store values, and some store references.

VariableKinds.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

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

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

Join with +

+ joins two strings into one new result.

Add Primitives

A primitive can join a String and become text.

Keep It with +=

+= stores the combined result back into the variable.

🔍 Concatenation Quiz (1/5)

What is printed? Pay close attention to when text first appears.

ConcatenationQuiz.java

🔍 Concatenation Quiz (2/5)

What is printed? Pay close attention to when text first appears.

ConcatenationQuiz.java

🔍 Concatenation Quiz (3/5)

What is printed? Pay close attention to when text first appears.

ConcatenationQuiz.java

🔍 Concatenation Quiz (4/5)

What is printed? Pay close attention to when text first appears.

ConcatenationQuiz.java

🔍 Concatenation Quiz (5/5)

What is printed? Pay close attention to when text first appears.

ConcatenationQuiz.java

📞 Calling Methods

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

StringMethods.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

Assume String word = "coding", then compare the common methods at a glance.

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

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.

🔤 Count Actual Characters

An escape sequence represents one character in the String.

EscapedCharacters.java
A
A
\"
"
B
B
\\
\
C
C
\n
↵newline
D
D

Each box holds 1 character, including the newline.
Count the characters in the String, not the symbols in the source code.

🔢 String Indexes

Indexes start at 0 and stop at length minus 1.

StringIndexes.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

What It Returns

An int index, or -1 if not found.

This Example

In "banana", "nan" begins at index 2.

Not Found

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

🔍 indexOf Quiz (1/4)

What value is printed?

IndexOfQuiz.java

🔍 indexOf Quiz (2/4)

What value is printed?

IndexOfQuiz.java

🔍 indexOf Quiz (3/4)

What value is printed?

IndexOfQuiz.java

🔍 indexOf Quiz (4/4)

What value is printed?

IndexOfQuiz.java

✂️ substring(from, to)

Take the part from from up to to - 1.

SubstringRangeExample.java
c
0
o
1
d
2
i
3
n
4
g
5
from = 1 · included
to = 4 · excluded

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

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

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.

🔍 substring Quiz (1/5)

What is printed?

SubstringQuiz.java

🔍 substring Quiz (2/5)

What is printed?

SubstringQuiz.java

🔍 substring Quiz (3/5)

What is printed?

SubstringQuiz.java

🔍 substring Quiz (4/5)

What is printed?

SubstringQuiz.java

🔍 substring Quiz (5/5)

What is printed?

SubstringQuiz.java

🟰 equals()

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

EqualsExample.java

What It Returns

A boolean: either true or false.

equals()

Compares the character sequence, so the example returns true.

Not ==

== compares references, not whether two strings contain the same text.

↕️ compareTo()

Use compareTo() when you want alphabetical order.

CompareToExample.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

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 (1/12)

What is printed?

StringQuiz.java

🔍 String Quiz (2/12)

What is printed?

StringQuiz.java

🔍 String Quiz (3/12)

What is printed?

StringQuiz.java

🔍 String Quiz (4/12)

What is printed?

StringQuiz.java

🔍 String Quiz (5/12)

What is printed?

StringQuiz.java

🔍 String Quiz (6/12)

What is printed?

StringQuiz.java

🔍 String Quiz (7/12)

What is printed?

StringQuiz.java

🔍 String Quiz (8/12)

What is printed?

StringQuiz.java

🔍 String Quiz (9/12)

What is printed?

StringQuiz.java

🔍 String Quiz (10/12)

What is printed?

StringQuiz.java

🔍 String Quiz (11/12)

What is printed?

StringQuiz.java

🔍 String Quiz (12/12)

What is printed?

StringQuiz.java

🧊 Immutability

String methods usually return a new result.

ImmutableString.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.

🔍 Immutability Quiz (1/2)

What is printed?

ImmutabilityQuiz.java

🔍 Immutability Quiz (2/2)

What is printed?

ImmutabilityQuiz.java

🚫 Null References

A reference can also point to no object.

NullExample.java
emptyName
→
""
real String object
nickname
⇢
null
no object

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 an empty String, but not on null.

⚠️ What Is an Exception?

Connect the runtime errors you have already seen.

⚠️

Exception ⊂ Runtime Error

An exception is a runtime error that interrupts the program's normal flow.

The code may compile successfully before the failure appears during execution.

Recognize the cause and result.
You do not need to write exception-handling code.

🧭 Cause and Exception

Match each operation to the exception it causes.

➗

ArithmeticException

Integer division by zero.

🫥

NullPointerException

Calling a method through a null reference.

⌨️

InputMismatchException

Scanner input does not match the expected type.

📍

StringIndexOutOfBoundsException

A String index is outside its valid range.

🔍 Runtime Quiz (1/4)

What is printed, or which exception occurs?

RuntimeQuiz.java

🔍 Runtime Quiz (2/4)

What is printed, or which exception occurs?

RuntimeQuiz.java

🔍 Runtime Quiz (3/4)

What is printed, or which exception occurs?

RuntimeQuiz.java

🔍 Runtime Quiz (4/4)

What is printed, or which exception occurs?

RuntimeQuiz.java

🌳 Java Class Family

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

🪄 Automatic Text Conversion

Primitive values can become text when they join a String.

StringBehindScenes.java

java.lang

String is built in, so Java makes it available automatically.

Value + String

When an int joins a String, Java converts the value to text.

📝 Homework

Complete it on time and submit it before the deadline.

🧭 Next Class

Check what comes next and arrive ready.

🔖 Summary

Key takeaways from this lesson.

Class vs Object

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

Reference Variables

A String variable stores a reference, which can also be null.

String Methods

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

Immutable Strings

String methods return new results, so save the result when you need it.

01 / 61