Object-Oriented Programming through Java · Strings, Threads, JDBC and JavaFX
Strings
In Object-Oriented Programming through Java because a seat label is a String — immutable text, CharSequence, and StringBuffer when the stub must grow.
"A12" is a String object, not a primitive. Once created it does not change: concatenation builds a new String. CharSequence is the interface String already implements. StringBuffer is a mutable buffer for a stub you append to from several steps — including from more than one thread.
- Object-Oriented Programming through Java
- Easy level
- 6 concepts
1A String is an object that holds characters
String is a class in java.lang. "A12" is a String object. It has methods: length, charAt, substring, equals.
It is not a char. A char is one 16-bit unit. A String is a sequence of them with a length.
Figure. "A12" is a String object, a sequence of characters with a length. length is 3. charAt(0) is A. It is not a char.
length 3; first character A
String seat = "A12";
int n = seat.length();
char row = seat.charAt(0);Length of A12
seat is "A12". What is seat.length() and seat.charAt(0)?
- seat.length()3
- seat.charAt(0)A
- seat.charAt(2)2
Pro tip. charAt uses an index from 0, the same rule as arrays.
2A String does not change in place
String is immutable: every method that looks like an edit returns a new String. seat.toUpperCase() does not alter seat unless you assign the result.
seat + " student" builds another object. The old "A12" is unchanged. That is why == after concat is not the original object.
Figure. seat.toUpperCase() does not alter seat unless you assign the result. The old A12 is unchanged. Every method that looks like an edit returns a new String.
How it works
- A String does not change in placeString methods return a new String. Assign if you want to keep the result.
Forget the assignment and nothing changes
String seat = "A12";
seat.toUpperCase();
// seat is still "A12"
seat = seat.toUpperCase();
// now seat is "A12" in upper case — a new object3CharSequence is the shared text contract
CharSequence is an interface: length, charAt, subSequence. String implements it. So do StringBuffer and StringBuilder.
A method that only needs to read characters should take CharSequence so a buffer or a String may both be passed.
Figure. CharSequence is the shared text contract: length, charAt, subSequence. String implements it. So do StringBuffer and StringBuilder. show("A12") passes a String.
How it works
- CharSequence is the shared text contractCharSequence is readable text. String is one implementation.
String is a CharSequence
void show(CharSequence text) {
System.out.println(text.length());
}
show("A12");4The methods the desk actually calls
equals compares characters. contains, startsWith and indexOf search. substring copies a slice. split cuts on a delimiter. trim drops edge spaces.
None of these change the original String. Read the return value.
No diagram — the idea is carried by the prose or the Java listing.
| Method | What it answers |
|---|---|
| equals | same characters |
| startsWith / contains | a prefix or a slice |
| substring | a copy of a range |
| split | pieces on a delimiter |
split returns a new array of Strings
String line = "A12,200";
String[] parts = line.split(",");
boolean student = line.startsWith("S");Split a stub line
line is "A12,200". parts = line.split(","). What is parts[0], parts[1], and line after the split?
- parts[0]A12
- parts[1]200
- line after splitA12,200 (unchanged)
Pro tip. split returns a new array. The original String is still the comma line.
Coding lab. Split the stub and print the seat runs in the app, with checks on your output.
5StringBuffer is a mutable, thread-safe buffer
StringBuffer holds characters you can append, insert or delete without building a new String each time. When the stub is finished, toString() gives a String.
Its methods are synchronized: two threads appending to the same buffer will not tear a character. StringBuilder is the faster unsynchronized twin for one thread.
Figure. StringBuffer appends A12, a space, and 200 in place. toString() then gives an immutable String. Prefer it when several threads share the buffer.
How it works
- StringBuffer is a mutable, thread-safe bufferStringBuffer mutates. toString() is the immutable snapshot. Prefer it when several threads share the buffer.
Append in place, then freeze to a String
StringBuffer stub = new StringBuffer();
stub.append("A12");
stub.append(' ');
stub.append(200);
String line = stub.toString();6== is the same object; equals is the same characters
For String, == asks whether both variables refer to the same object. equals asks whether the characters match.
Literals like "A12" may be interned and share an object. new String("A12") is a second object. The desk compares seats with equals, not ==.
Figure. a is the literal A12. b is new String("A12") — a second object. == is false. equals is true: the characters match. The desk compares seats with equals, not ==.
== may be false; equals is true
String a = "A12";
String b = new String("A12");
System.out.println(a == b);
System.out.println(a.equals(b));Same letters, two objects
a is the literal "A12". b is new String("A12"). What is a == b, and what is a.equals(b)?
- a == bfalse
- a.equals(b)true
Pro tip. new String always allocates. The desk asks equals so a typed seat still matches a seat from a file.
Notes
- In Object-Oriented Programming through Java because a seat label is a String — immutable text, CharSequence, and StringBuffer when the stub must grow.
- String implements CharSequence. == on two Strings tests whether they are the same object; equals tests the characters.
- StringBuffer is mutable and synchronized. StringBuilder is the unsynchronized sibling you will meet in later APIs.
Exam traps & shortcuts
- "A12" == new String("A12") is often false. Use equals.
- A loop that does s = s + next builds many discarded String objects. A buffer appends in place.
Recap
String is an immutable object. CharSequence is the read contract. StringBuffer mutates. equals compares characters.
- immutable
- Edits return a new String. Assign if you care.
- CharSequence
- String, StringBuffer and StringBuilder all keep it.
- equals not ==
- Same letters, possibly two objects.
Practise Strings
Reading is free and needs no account. Practice, mocks and progress live in the app.
- Timed mocks scored with the real marking scheme
- Readiness tracked per topic, kept on your device