Read without ads and support Scribd by becoming a Scribd Premium Reader.
 
6
Strings, I/O,Formatting,and Parsing
CertifiCation objeCtives
 
l
 
Using String, StringBuilder, andStringBuer
l
 
File I/O using the java.io package
l
 
Serialization using the java.iopackage 
l
 
Working with Dates, Numbers,and Currencies 
l
 
Using Regular Expressions
3
Two-Minute Drill
Q&A
Sel Test
 
412
Chapter 6: Strings, I/O, Formatting, and Parsing
T
his chapter ocuses on the various API-related topics that were added to the exam or Java 5. J2SE comes with an enormous API, and a lot o your work as a Java programmerwill revolve around using this API. The exam team chose to ocus on APIs or I/O,ormatting, and parsing. Each o these topics could ill an entire book. Fortunately, you won't haveto become a total I/O or regex guru to do well on the exam. The intention o the exam team wasto include just the basic aspects o these technologies, and in this chapter we cover
more
thanyou'll need to get through the String, I/O, ormatting, and parsing objectives on the exam.
CertifiCation objeCtive
s, sbld, d sb (exm oc 3.1)
3.1 Discuss the differences between the String, StringBuilder, and StringBuffer classes.
Everything you needed to know about Strings in the SCJP 1.4 exam, you'll need toknow for the SCJP 5 exam…plus, Sun added the
StringBuilder
class to the API, toprovide faster, non-synchronized StringBuffer capability. The StringBuilder class hasexactly the same methods as the old StringBuffer class, but StringBuilder is fasterbecause its methods aren't synchronized. Both classes give you String-like objectsthat handle some of the String class's shortcomings (like immutability).
th s Cl
This section covers the String class, and the key concept to understand is that oncea String object is created, it can never be changed—so what is happening when aString object seems to be changing? Let's find out.
s a imml oc
We'll start with a little background information about strings. You may not needthis for the test, but a little context will help. Handling "strings" of characters is afundamental aspect of most programming languages. In Java, each character in astring is a 16-bit Unicode character. Because Unicode characters are 16 bits (not
 
The String Class (Exam Objective 3.1)
413
the skimpy 7 or 8 bits that ASCII provides), a rich, international set of characters iseasily represented in Unicode.In Java, strings are objects. Just like other objects, you can create an instance of aString with the
new
keyword, as follows:
String s = new String();
This line of code creates a new object of class String, and assigns it to thereference variable
s
. So far, String objects seem just like other objects. Now, let'sgive the String a value:
s = "abcdef";
As you might expect, the String class has about a zillion constructors, so you canuse a more efficient shortcut:
String s = new String("abcdef");
And just because you'll use strings all the time, you can even say this:
String s = "abcdef";
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 avalue of 
"abcdef"
, and assign it to a reference variable
s
. Now let's say that youwant a second reference to the String object referred to by
s
:
String s2 = s; // refer s2 to the same String as s
So far so good. String objects seem to be behaving just like other objects, sowhat's all the fuss about?…Immutability! (What the heck is immutability?) Onceyou have assigned a String a value, that value can never change— 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 isnot, so to continue with our previous example:
s = s.concat(" more stuff"); // the concat() method 'appends'// a literal to the end
Search History:
Searching...
Result 00 of 00
00 results for result for
  • p.
  • Notes
    Load more