Thursday, January 13, 2011

Passed The Exam

I passed the exam, hope this blog will help you study!

Sunday, November 7, 2010

Online Study

You need to remember the following rules for format specification:

1. The general syntax is %[arg_index$][flags][width][.precision]conversion char

2. A format specification must start with a % and end with a conversion char ( b boolean, c char,  d integer, f floating point, and s string). Anything before a % and after the conversion character is printed as it is. For example, printf("xxx%1$dyyy%2$dzzz", 10, 20) will be print xxx10yyy20zzz. Arg-index, flags, width, and .precision are all optional.

Friday, September 17, 2010

Code to Understand Objective 6.1

Courtesy of SCJP Sun® Certified Programmer for Java™ 6 Study Guide Exam (310-065) (9780071591065)
Courtesy of

6.1 Given a design scenario, determine which collection classes and/or interfaces should be used to properly implement that design, including the use of the Comparable interface.

There are two ways to traverse collections:

(1) with the for-each construct

(2) by using Iterators

The following program takes the words in its argument list and prints out any duplicate words, the number of distinct words, and a list of the words with duplicates eliminated.

Note that the code always refers to the Collection by its interface type (Set) rather than by its implementation type (HashSet). This is a strongly recommended programming practice because it gives you the flexibility to change implementations merely by changing the constructor.

If you want the program to print the word list in alphabetical order, merely change the Set's implementation type from HashSet to TreeSet.

To calculate the union, intersection, or set difference of two sets nondestructively (without modifying either set), the caller must copy one set before calling the appropriate bulk operation. The following are the resulting idioms.

Let's revisit the FindDups program. Suppose you want to know which words in the argument list occur only once and which occur more than once, but you do not want any duplicates printed out repeatedly. This effect can be achieved by generating two sets — one containing every word in the argument list and the other containing only the duplicates. The words that occur only once are the set difference of these two sets, which we know how to compute. Here's how the resulting program looks.

Produce a third List consisting of the second list appended to the first.

Code to Understand Objective 6.2

Courtesy of SCJP Sun® Certified Programmer for Java™ 6 Study Guide Exam (310-065) (9780071591065)

6.2 Distinguish between correct and incorrect overrides of corresponding hashCode and equals methods, and explain the difference between == and the equals method.

To override toString() method in a class:

Overriding equals()

Heartbeat: "The equals() method in class Object uses only the == operator for comparisons, so unless you override equals(), two objects are considered equal only if the two references refer to the same object."

Overriding hashCode()

Hashing retrieval is a two-step process.

   1. Find the right bucket (using hashCode())
   2. Search the bucket for the right element (using equals() ).

transient variables can really mess with your equals() and hashCode() implementations. Keep variables non-transient or, if they must be marked transient, don’t use them to determine hashcodes or equality.

Saturday, September 11, 2010

Code to Understand Objective 4.3

Courtesy of SCJP Sun® Certified Programmer for Java™ 6 Study Guide Exam (310-065) (9780071591065)

4.3 Given a scenario, write code that makes appropriate use of object locking to protect static or instance variables from concurrent access problems.

The following code demonstrates what happens when two different threads are accessing the same account data. Imagine that two people each have a checkbook for a single checking account (or two people each have ATM cards, but both cards are linked to only one account).

Prevent "race condition" by using synchronized

Prevent “race condition,” where multiple threads can access the same resource (typically an object’s instance variables), and can produce corrupted data if one thread “races in” too quickly before an operation that should be “atomic” has completed.

makeWithdrawal is synchronized.