Friday, August 6, 2010

Inner Classes:

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

Chapter 8, Question 12

This program will NOT compile because Sorter is being invoked from a static context, i.e. the main method. If Sorter was also static, then the program would compile and the output would be:

nickel button key lint
nickel lint key button

Notice how the second line of output prints the contents of sa in reverse order. This is because Sorter was implemented to reverse the order of the Arrays. Notice how line 13 says

return b.compareTo(a);

rather than

return a.compareTo(b);

even though the arguments of compare are

(String a, String b)

For example, if the program executes,

compare("nickel", "button");

The compare function will leave the order of "nickel","button" alone because

return "button".compareTo("nickel");

returns negative, so no action is taken to change the sequence

No comments:

Post a Comment