Certification Summary


Strings The most important thing to remember about Strings is that String objects are immutable, but references to Strings are not! You can make a new String by using an existing String as a starting point, but if you don't assign a reference variable to the new String it will be lost to your program—you will have no way to access your new String. Review the important methods in the String class.

The StringBuilder class was added in Java 5. It has exactly the same methods as the old StringBuffer class, except StringBuilder's methods aren't thread-safe. Because StringBuilder's methods are not thread safe, they tend to run faster than StringBuffer methods, so choose StringBuilder whenever threading is not an issue. Both StringBuffer and StringBuilder objects can have their value changed over and over without having to create new objects. If you're doing a lot of string manipulation, these objects will be more efficient than immutable String objects, which are, more or less, "use once, remain in memory forever." Remember, these methods ALWAYS change the invoking object's value, even with no explicit assignment.

File I/O Remember that objects of type File can represent either files or directories, but that until you call createNewFile() or mkDir() you haven't actually created anything on your hard drive. Classes in the java.io package are designed to be chained together. It will be rare that you'll use a FileReader or a FileWriter without "wrapping" them with a BufferedReader or BufferedWriter object, which gives you access to more powerful, higher-level methods. As of Java 5, the PrintWriter class has been enhanced with advanced append(), format(), and printf() methods, and when you couple that with new constructors that allow you to create PrintWriters directly from a String name or a File object, you may use BufferWriters a lot less.

Serialization Serialization lets you save, ship, and restore everything you need to know about a live object. And when your object points to other objects, they get saved too. The java.io.ObjectOutputStream and java.io.ObjectlnputStream classes are used to serialize and deserialize objects. Typically you wrap them around instances of FileOutputStream and FilelnputStream, respectively.

The key method you invoke to serialize an object is writeObject(), and to deserialize an object invoke readMethod(). In order to serialize an object, it must implement the Serializable interface. Mark instance variables transient if you don't want their state to be part of the serialization process. You can augment the serialization process for your class by implementing writeobject() and readObject(). If you do that, an embedded call to defaultReadobject() and defaultWriteObject() will handle the normal serialization tasks, and you can augment those invocations with manual reading from and writing to the stream.

If a superclass implements Serializable then all of its subclasses do too. If a superclass doesn't implement Serializable, then when a subclass object is deserialized the unserializable superclass's constructor runs—be careful! Finally, remember that serialization is about instances, so static variables aren't serialized.

Dates, Numbers, and Currency Remember that the Sun objective is a bit misleading, and that you'll have to understand the basics of five related classes: java.util.Date, java.util.Calendar, java.util.Locale, java.text.DateFormat, and java. text.NumberFormat. A Date is the number of milliseconds since Jan. 1, 1970, stored in a long. Most of Date's methods have been deprecated, so use the Calendar class for your date-manipulation tasks. Remember that in order to create instances of Calendar, DateFormat, and NumberFormat, you have to use static factory methods like getinstance(). The Locale class is used with DateFormat and NumberFormat to generate a variety of output styles that are language and/or country specific.

Parsing,Tokenizing, and Formatting To find specific pieces of data in large data sources, Java provides several mechanisms that use the concepts of regular expressions (regex). regex expressions can be used with the java.util.regex package's Pattern and Matcher classes, as well as with java.util.Scanner and with the String.split() method. When creating regex patterns, you can use literal characters for matching or you can use metacharacters, that allow you to match on concepts like "find digits" or "find whitespace." regex provides quantifiers that allow you to say things like "find one or more of these things in a row." You won't have to understand the Matcher methods that facilitate replacing strings in data.

Tokenizing is splitting delimited data into pieces. Delimiters are usually as simple as a comma, but they can be as complex as any other regex pattern. The java.util.Scanner class provides full tokenizing capabilities using regex, and allows you to tokenize in a loop so that you can stop the tokenizing process at any point. String.split() allows full regex patterns for tokenizing, but tokenizing is done in one step, hence large data sources can take a long time to process.

Formatting data for output can be handled by using the Formatter class, or more commonly by using the new PrintStream methods format() and printf(). Remember format() and printf(). Remember format() and print f() behave identically. To use these methods, you create a format string that is associated with every piece of data you want to format. You need to understand the subset of format string conventions we covered in the chapter, and you need to remember that if your format string specifies a conversion character that doesn't match your data type, an exception will be thrown.




SCJP Sun Certified Programmer for Java 5 Study Guide Exam 310-055
SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310-055) (Certification Press)
ISBN: 0072253606
EAN: 2147483647
Year: 2006
Pages: 131

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net