Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Standard view
Full view
of .
Look up keyword
Like this
0 of .
Results for:
No results containing your search query
P. 1
Chap 06

Chap 06

Ratings: (0)|Views: 0|Likes:
Published by api-3700560

More info:

Published by: api-3700560 on Oct 18, 2008
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less






Strings, I/O, Formatting, and Parsing

CertifiCation objeCtives
Using String, StringBuilder, and
File I/O using the java.io package
Serialization using the java.io
Working with Dates, Numbers,
and Currencies
Using Regular Expressions
3 Two-Minute Drill
Q&ASel\ue000 Test
412 Chapter 6: Strings, I/O, Formatting, and Parsing
his chapter \ue000ocuses on the various API-related topics that were added to the exam \ue000or
Java 5. J2SE comes with an enormous API, and a lot o\ue000 your work as a Java programmer

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.

CertifiCation objeCtive
s\ue00b\ue009\ue005\ue007\ue004, s\ue00b\ue009\ue005\ue007\ue004b\ue00c\ue005ld\ue002\ue009, \ue000\ue007d s\ue00b\ue009\ue005\ue007\ue004b\ue00c\ue003\ue003\ue002\ue009 (ex\ue000m o\ue001\ue006\ue002c\ue00b\ue005\ue00d\ue002 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 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).

th\ue002 s\ue00b\ue009\ue005\ue007\ue004 Cl\ue000\ue00a\ue00a

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.

s\ue00b\ue009\ue005\ue007\ue004\ue00a a\ue009\ue002 imm\ue00c\ue00b\ue000\ue001l\ue002 o\ue001\ue006\ue002c\ue00b\ue00a

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

The String Class (Exam Objective 3.1)413
the skimpy 7 or 8 bits that ASCII provides), a rich, international set of characters is
easily represented in Unicode.
In Java, strings are objects. Just like other objects, you can create an instance of a
String with thenew keyword, as follows:
String s = new String();

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:

s = "abcdef";
As you might expect, the String class has about a zillion constructors, so you can
use 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 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:

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, 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:

s = s.concat(" more stuff"); // the concat() method 'appends'
// a literal to the end

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->