Strings, I/O, Formatting, and Parsing
will revolve around using this API. The exam team chose to \ue000ocus on APIs \ue000or I/O,
\ue000ormatting, and parsing. Each o\ue000 these topics could \ue000ill an entire book. Fortunately, you won't have
to become a total I/O or regex guru to do well on the exam. The intention o\ue000 the exam team was
to include just the basic aspects o\ue000 these technologies, and in this chapter we coverm o re than
you'll need to get through the String, I/O, \ue000ormatting, and parsing objectives on the exam.
Everything you needed to know about Strings in the SCJP 1.4 exam, you'll need to
know for the SCJP 5 exam\u2026plus, Sun added theStringBuilder class to the API, to
provide faster, non-synchronized StringBuffer capability. The StringBuilder class has
exactly the same methods as the old StringBuffer class, but StringBuilder is faster
because its methods aren't synchronized. Both classes give you String-like objects
that handle some of the String class's shortcomings (like immutability).
This section covers the String class, and the key concept to understand is that once
a String object is created, it can never be changed\u2014so what is happening when a
String object seems to be changing? Let's find out.
We'll start with a little background information about strings. You may not need this for the test, but a little context will help. Handling "strings" of characters is a fundamental aspect of most programming languages. In Java, each character in a string is a 16-bit Unicode character. Because Unicode characters are 16 bits (not
This line of code creates a new object of class String, and assigns it to the
reference variables. So far, String objects seem just like other objects. Now, let's
give the String a value:
There are some subtle differences between these options that we'll discuss later, but what they have in common is that they all create a new String object, with a value of"abcdef", and assign it to a reference variables. Now let's say that you want a second reference to the String object referred to bys:
So far so good. String objects seem to be behaving just like other objects, so
what's all the fuss about?\u2026Immutability! (What the heck is immutability?) Once
you have assigned a String a value, that value can never change\u2014 it's immutable,
frozen solid, won't budge, fini, done. (We'll talk about why later, don't let us forget.)
The good news is that while the String object is immutable, its reference variable is
not, so to continue with our previous example:
This action might not be possible to undo. Are you sure you want to continue?