Summary
In this chapter, we looked at popular classes from the Java Core API. We started with Scanner
, a useful class for reading input. Scanner
can be directed to read from a file, a String
object, or the keyboard. Reading from the keyboard is particularly useful for dealing with user input.
We examined the String
class and its API. We saw how String
literals use the string constant pool to save on memory. We examined an important property of String
objects, namely immutability. A String
object, once created, cannot be changed.
Next, we examined StringBuilder
and its API. We discussed that StringBuilder
is a mutable type and thus, there is only ever one object in memory.
Given that String
is immutable but StringBuilder
is mutable, we presented a detailed example with both code and supporting diagrams to compare and contrast String
and StringBuilder
.
This led to a discussion on how to create our own custom immutable types. We examined a checklist of steps you must perform...