Fast Track To Sun Certified Java Programmer (SCJP) 5.0 Upgrade Exam
Fast Track To Sun Certified Java Programmer (SCJP) 5.0 Upgrade Exam
Fast Track To Sun Certified Java Programmer (SCJP) 5.0 Upgrade Exam
Certified Java
Programmer (SCJP)
5.0 Upgrade Exam
Copyright © 2006
Ka Iok 'Kent' Tong
Foreword
Acknowledgments
I'd like to thank:
• Helena Lei for proofreading this book.
• Eugenia Chan Peng U for doing the book cover and the layout design.
4 Fast Track to Sun Certified Java Programmer (SCJP) 5.0
Table of Contents
Foreword...................................................................................... 3
Learn the new features in Java SE 5.0................................... 3
Target audience and prerequisites.......................................... 3
Acknowledgments................................................................... 3
Chapter 1 Autoboxing.................................................................. 9
What's in this chapter?.......................................................... 10
Autoboxing............................................................................ 10
Auto unboxing....................................................................... 10
Other contexts....................................................................... 11
Autoboxing and method name overloading........................... 11
Summary............................................................................... 12
Review questions.................................................................. 13
Answers to review questions................................................. 14
Mock exam............................................................................ 15
Answers to the mock exam................................................... 17
Chapter 2 Generics................................................................... 19
What's in this chapter?.......................................................... 20
Using generics....................................................................... 20
Parameterized types are compile-time properties of variables..
21
Assignment compatibility between parameterized type
variables................................................................................ 23
Comparing a List to an array................................................. 24
Wildcard type......................................................................... 25
Constraining a type parameter.............................................. 27
Writing generic methods........................................................ 28
Specifying a lower bound...................................................... 28
Use a wildcard or a type variable?........................................ 29
Integrating legacy code with generic-aware code..................30
Generics and method overriding........................................... 32
Generics and method name overloading.............................. 34
Common mistakes................................................................. 34
Summary............................................................................... 35
Review questions.................................................................. 36
Answers to review questions................................................. 38
Mock exam............................................................................ 42
Fast Track to Sun Certified Java Programmer (SCJP) 5.0 5
Summary............................................................................... 80
Review questions.................................................................. 81
Answers to review questions................................................. 82
Mock exam............................................................................ 83
Answers to the mock exam................................................... 85
Chapter 7 Static Imports............................................................ 87
What's in this chapter?.......................................................... 88
Static imports......................................................................... 88
Static imports and enums...................................................... 89
Summary............................................................................... 89
Review questions.................................................................. 90
Answers to review questions................................................. 91
Mock exam............................................................................ 92
Answers to the mock exam................................................... 93
Chapter 8 Covariant Return Types............................................ 95
What's in this chapter?.......................................................... 96
Narrowing the return type...................................................... 96
Summary............................................................................... 97
Review questions.................................................................. 98
Answers to review questions................................................. 99
Mock exam.......................................................................... 100
Answers to the mock exam................................................. 101
Chapter 9 Java I/O.................................................................. 103
What's in this chapter?........................................................ 104
Using the File class............................................................. 104
InputStream......................................................................... 104
OutputStream...................................................................... 105
Reading and writing primitive values................................... 106
Reading and writing strings................................................. 106
Buffered reading/writing...................................................... 107
Reading and writing primitive data as text strings............... 108
Reading and writing objects................................................ 108
Versioning in object serialization......................................... 110
Summary............................................................................. 111
Review questions................................................................ 112
Answers to review questions............................................... 114
Mock exam.......................................................................... 116
Answers to the mock exam................................................. 119
Chapter 10 Formatting and Parsing.........................................121
Fast Track to Sun Certified Java Programmer (SCJP) 5.0 7