Two-Minute Drill


Here are some of the key points from the certification objectives in this chapter.

Using String, StringBuffer, and StringBuilder (Objective 3.1)

q  

String objects are immutable, and String reference variables are not.

q  

If you create a new String without assigning it, it will be lost to your program.

q  

If you redirect a String reference to a new String, the old String can be lost.

q  

String methods use zero-based indexes, except for the second argument of substring().

q  

The String class is final—its methods can't be overridden.

q  

When the JVM finds a String literal, it is added to the String literal pool.

q  

Strings have a method: length(); arrays have an attribute named length.

q  

The StringBuffer's API is the same as the new StringBuilder's API, except that StringBuilder's methods are not synchronized for thread safety.

q  

StringBuilder methods should run faster than StringBuffer methods.

q  

All of the following bullets apply to both StringBuffer and StringBuilder:

q  

They are mutable—they can change without creating a new object.

q  

StringBuffer methods act on the invoking object, and objects can change without an explicit assignment in the statement.

q  

StringBuffcr equals() is not overridden; it doesn't compare values.

q  

Remember that chained methods arc evaluated from left to right.

q  

String methods to remember: charAt(),concat(),equalslgnoreCase(),length(),replace(),substring(),toLowerCase(),toString(), toUp-perCase(), and trim().

q  

Stringbuffer methods to remember: append(),delete(),insert(),reverse(), and toString().

File I/O (Objective 3.2)

q  

The classes you need to understand in java.io are File, FileReader, BufferedReader, FileWriter, BufferedWriter, and PrintWriter.

q  

A new File object doesn't mean there's a new file on your hard drive.

q  

File objects can represent either a file or a directory.

q  

The File class lets you manage (add, rename, and delete) files and directories.

q  

The methods createNewFile() and mkDir() add entries to your file system.

q  

FileWriter and FileReader are low-level I/O classes. You can use them to write and read files, but they should usually be wrapped.

q  

Classes in java.io are designed to be "chained" or "wrapped." (This is a common use of the decorator design pattern.)

q  

It's very common to "wrap" a BufferedReader around a FileReader, to get access to higher-level. (more convenient) methods.

q  

It's very common to "wrap" a BufferedWriter around a FileWriter, to get access to higher-level (more convenient) methods.

q  

PrintWriters can be used to wrap other Writers, but as of Java 5 they can be built directly from Files or Strings.

q  

Java 5 PrintWriters have new append(), format(), and printf() methods.

Serialization (Objective 3.3)

q  

The classes you need to understand are all in the java.io package; they include: ObjectOutputStream and ObjectInputStream primarily, and FileOutputStream and FilelnputStream because you will use them to create the low-level streams that the ObjectXxxStream classes will use.

q  

A class must implement the Serializable interface before its objects can be serialized.

q  

The Objectoutputstream.writeobject() method serializes objects, and the Objectinputstream.readobject() method deserializes objects.

q  

If you mark an instance variable transient, it will not be serialized even thought the rest of the object's state will be.

q  

You can supplement a class's automatic serialization process by implementing the writeObject() and readObject() methods, If you do this, embedding calls to defaultWriteObject() and defaultReadQbject(), respectively, will handle the part of serialization that happens normally.

q  

If a superclass implements Serializable, then its subclasses do automatically.

q  

If a superclass doesn't implement Serializable, then when a subclass object is deserialized, the superclass constructor will run.

q  

DataInputStream and DataOutputStream aren't actually on the exam, in spite of what the Sun objectives say.

Dates, Numbers, and Currency (Objective 3.4)

q  

The classes you need to understand are java.util.Date, java.util.Calendar, java.text.DateFormat, java.text.NumberFormat, and java.util.Locale.

q  

Most of the Date class's methods have been deprecated.

q  

A Date is stored as a long, the number of milliseconds since January 1, 1970.

q  

Date objects are go-betweens the Calendar and Locale classes.

q  

The Calendar provides a powerful set of methods to manipulate dates, performing tasks such as getting days of the week, or adding some number of months or years (or other increments) to a date.

q  

Create Calendar instances using static factory methods (getInstance()).

q  

The Calendar methods you should understand are add(), which allows you to add or subtract various pieces (minutes, days, years, and so on) of dates, and roll(), which works like add() but doesn't increment a date's bigger pieces. (For example: adding 10 months to an October date changes the month to August, but doesn't increment the Calendar's year value.)

q  

DateFormat instances are created using static factory methods (getInstance() and getDateInstance()).

q  

There are several format "styles" available in the DateFormat class.

q  

DateFormat styles can be applied against various Locales to create a wide array of outputs for any given date.

q  

The DateFormat.format() method is used to create Strings containing properly formatted dates.

q  

The Locale class is used in conjunction with DateFormat and NumberFormat.

q  

Both DateFormat and NumberFormat objects can be constructed with a specific, immutable Locale.

q  

For the exam you should understand creating Locales using language, or a combination of language and country.

Parsing,Tokenizing, and Formatting (Objective 3.5)

q  

regex is short for regular expressions, which are the patterns used to search for data within large data sources.

q  

regex is a sub-language that exists in Java and other languages (such as Perl).

q  

regex lets you to create search patterns using literal characters or metacharacters. Metacharacters allow you to search for slightly more abstract data like "digits" or "whitespace".

q  

Study the \d, \s, \w, and . metacharacters

q  

regex provides for quantifiers which allow you to specify concepts like: "look for one or more digits in a row."

q  

Study the ?, *, and + greedy quantifiers.

q  

Remember that metacharacters and Strings don't mix well unless you remember to "escape" them properly. For instance string s = "\\d";

q  

The Pattern and Matcher classes have Java's most powerful regex capabilities.

q  

You should understand the Pattern compile() method and the Matcher matcher(),pattern(),find(),start(), and group() methods.

q  

You WON'T need to understand Matcher's replacement-oriented methods.

q  

You can use java.util.Scanner to do simple regex searches, but it is primarily intended for tokcnizing.

q  

Tokenizing is the process of splitting delimited data into small pieces.

q  

In tokenizing, the data you want is called tokens, and the strings that separate the tokens are called delimiters.

q  

Tokenizing can be done with the Scanner class, or with String.split().

q  

Delimiters are single characters like commas, or complex regex expressions.

q  

The Scanner class allows you to tokenize data from within a loop, which allows you to stop whenever you want to.

q  

The Scanner class allows you to tokenize Strings or streams or files.

q  

The string.split() method tokenizes the entire source data all at once, so large amounts of data can be quite slow to process.

q  

New to Java 5 are two methods used to format data for output. These methods are format() and printf(). These methods are found in the PrintStream class, an instance of which is the out in system. out.

q  

The format() and print f() methods have identical functionality.

q  

Formatting data with printf() (or format()) is accomplished using formatting strings that are associated with primitive or string arguments.

q  

The format() method allows you to mix literals in with your format strings.

q  

The format string values you should know are

q  

Flags: -,+, 0, ",", and (

q  

Conversions: b, c, d, f, and s

q  

If your conversion character doesn't match your argument 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