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.

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.

Calling Methods

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

StringMethods.java
java

Use Dot

Use the dot to call a method on one object.

length()

Returns the number of characters in the string.

substring()

Returns part of the string as a new result.

indexOf()

Finds where a piece of text first appears.

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 Quiz

Pick the correct result.

Program.java
java

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.

Immutability Quiz

What is printed now?

What is printed by the first System.out.println(word) call?

Quiz.java
java

null

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.

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.

Creating Objects

Use new to create an object. For String, quotes are the shorter form.

Calling Methods

Use the dot operator to call methods, and save the new result when needed.

After-Class
Assignment

String Explorer

  • 1. Create one String variable.
  • 2. Print its length().
  • 3. Print one substring() result.

Immutable Check

  • 1. Create a string such as "java".
  • 2. Call one string method without reassigning the result.
  • 3. Reassign the returned value and print the difference.