Meet your first object in Java.
One machine can build many different robots.
Use the same machine, but change the settings each time.
A class is the plan, and an object is one real example.
The machine plan tells the factory what kind of robot to build.
One finished robot is a real instance built from that plan.
The class is String.
One object is "Hello".
Some variables store values, and some store references.
int, double, and boolean store the value itself.
A reference variable stores how Java finds the object.
String is your first important reference type in AP CSA.
Primitive values sit in the variable directly; reference variables point to the object.
Now treat text in Java as an object.
String is the class name for this kind of text object.
new String("Hello") creates one real string object.
You can call methods such as length() on it.
Start with new, then compare the shorter form.
new tells Java to create a fresh object.
A constructor helps Java create that object.
"Hello" is the shorter form for the same kind of object.
Use + to build a new string from smaller pieces.
+ joins two strings into one new result.
A primitive can join a String and become text.
+= stores the combined result back into the variable.
What is printed? Pay close attention to when text first appears.
What is printed? Pay close attention to when text first appears.
What is printed? Pay close attention to when text first appears.
What is printed? Pay close attention to when text first appears.
What is printed? Pay close attention to when text first appears.
Use the dot operator to call a method on one object.
A method call begins with one object such as word.
The dot connects that object to one method.
After the dot, write the method name with parentheses.
Assume String word = "coding", then compare the common methods at a glance.
Returns the number of characters.
word.length() -> 6
Returns the first index of str, or -1.
word.indexOf("d") -> 2
Returns the part from from to to - 1.
word.substring(1, 4) -> "odi"
Returns everything from from to the end.
word.substring(2) -> "ding"
Checks whether two strings have the same text.
word.equals("coding") -> true
Compares two strings in alphabetical order.
word.compareTo("dog") -> less than 0
Splits the string around the delimiter.
word.split("i") -> {"cod", "ng"}
Use length() when you need the number of characters.
An int with the string's character count.
"coding" has 6 characters.
Counting characters before you loop or slice.
An escape sequence represents one character in the String.
A\"B\\C\nD Each box holds 1 character, including the newline.
Count the characters in the String, not the symbols in the source code.
Indexes start at 0 and stop at length minus 1.
The first character is at index 0.
The last character is at length() - 1.
Index Error
Going past the range throws StringIndexOutOfBoundsException.
Use indexOf() to find where a piece of text first appears.
An int index, or -1 if not found.
In "banana", "nan" begins at index 2.
If the text is missing, Java gives back -1.
What value is printed?
What value is printed?
What value is printed?
What value is printed?
Take the part from from up to to - 1.
The character at from is included.
The character at to is not included.
substring(1, 4) gives "odi".
Take everything from one index to the end.
You only choose where the new string begins.
Java keeps every character after that point.
substring(2) gives "ding".
Use substring(index, index + 1) to make a one-letter string.
Start at the one character you want to take.
End at index + 1 so only one character is included.
The result is still a String, just with length 1.
What is printed?
What is printed?
What is printed?
What is printed?
What is printed?
Use equals() to check whether two strings have the same text.
A boolean: either true or false.
Compares the character sequence, so the example returns true.
== compares references, not whether two strings contain the same text.
Use compareTo() when you want alphabetical order.
The first string comes earlier alphabetically.
The two strings are alphabetically the same.
The first string comes later alphabetically.
Use split() to break one string into smaller pieces.
Java splits the string at each delimiter.
The result is a String[] with several pieces.
Splitting on "i" gives "cod" and "ng".
We will study split() more later, so just keep the big idea for now.
What is printed?
What is printed?
What is printed?
What is printed?
What is printed?
What is printed?
What is printed?
What is printed?
What is printed?
What is printed?
What is printed?
What is printed?
String methods usually return a new result.
word.substring(0, 3) alone does not replace the old string.
Use word = word.substring(0, 3) to keep the new text.
What is printed?
What is printed?
A reference can also point to no object.
"" is a real String object, so its length is 0.
null means the variable is connected to no object.
Java can call length() on an empty String, but not on null.
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.
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.
What is printed, or which exception occurs?
What is printed, or which exception occurs?
What is printed, or which exception occurs?
What is printed, or which exception occurs?
As a supplement, see how familiar classes fit into one larger family.
Primitive values can become text when they join a String.
String is built in, so Java makes it available automatically.
When an int joins a String, Java converts the value to text.
Complete it on time and submit it before the deadline.
Check what comes next and arrive ready.
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.