Fast Track To Sun Certified Java Programmer (SCJP) 5.0 Upgrade Exam

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Fast Track to Sun

Certified Java
Programmer (SCJP)
5.0 Upgrade Exam
Copyright © 2006
Ka Iok 'Kent' Tong

Publisher: TipTec Development


Author's email: [email protected]
Book website: http://www.agileskills2.org
Notice: All rights reserved. No part of this publication may be
reproduced, stored in a retrieval system or transmitted, in
any form or by any means, electronic, mechanical,
photocopying, recording, or otherwise, without the prior
written permission of the publisher.
Edition: First edition 2006
Fast Track to Sun Certified Java Programmer (SCJP) 5.0 3

Foreword

Learn the new features in Java SE 5.0


If you'd like to learn the new features in Java SE 5.0 and pass the Sun
Certified Java Programmer Upgrade Exam (CX-310-056), then this book is for
you. Why?
• It covers all the Java SE 5.0 new features covered in the exam. You don't
need to read about the features you already know.
• It is clear & concise. No need to go through hundreds of pages.
• I have passed the exam. So I know what you'll be up against.
• It includes 117 review questions and mock exam questions.
• Many working code fragments are used to show the semantics of the code
construct concerned.
• The first 30 pages are freely available on http://www.agileskills2.org. You
can judge it yourself.

Target audience and prerequisites


This book is suitable for those who would like to:
• Learn the new features in Java SE 5.0; or
• Take the Sun Certified Java Programmer Upgrade Exam.
In order to understand what's in the book, you need to know the basics of
Java. In order to take the exam, you also need to have passed a previous
version of the exam.

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

Answers to the mock exam................................................... 45


Chapter 3 For-each loop............................................................ 47
What's in this chapter?.......................................................... 48
For-each loop........................................................................ 48
Summary............................................................................... 50
Review questions.................................................................. 51
Answers to review questions................................................. 52
Mock exam............................................................................ 53
Answers to the mock exam................................................... 54
Chapter 4 Manipulating Collections........................................... 55
What's in this chapter?.......................................................... 56
Sorting and searching a List.................................................. 56
Sorting and searching an array............................................. 57
Converting a Collection into an array.................................... 57
Converting an array into a List............................................... 58
Summary............................................................................... 59
Review questions.................................................................. 60
Answers to review questions................................................. 61
Mock exam............................................................................ 62
Answers to the mock exam................................................... 63
Chapter 5 Variable Arity Parameters......................................... 65
What's in this chapter?.......................................................... 66
Using a vararg....................................................................... 66
Vararg and method name overloading.................................. 66
Vararg and method overriding............................................... 67
Summary............................................................................... 68
Review questions.................................................................. 69
Answers to review questions................................................. 70
Mock exam............................................................................ 71
Answers to the mock exam................................................... 72
Chapter 6 Enum........................................................................ 73
What's in this chapter?.......................................................... 74
Creating an enum class......................................................... 74
Converting between an enum value and a string.................. 76
Comparing enum values....................................................... 76
Iterating all the values in an enum......................................... 76
Building a set of enum values............................................... 77
Adding data and behaviors to an enum................................. 77
The Enum base class............................................................ 78
6 Fast Track to Sun Certified Java Programmer (SCJP) 5.0

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

What's in this chapter?........................................................ 122


Locale.................................................................................. 122
Formatting and parsing numbers........................................ 123
Formatting and parsing dates.............................................. 125
Formatting several values................................................... 127
Formatter............................................................................. 127
Using regular expressions................................................... 130
Parsing text data................................................................. 134
Summary............................................................................. 134
Review questions................................................................ 136
Answers to review questions............................................... 139
Mock exam.......................................................................... 142
Answers to the mock exam................................................. 145
Chapter 11 Preparing for the Exam......................................... 147
Exam format........................................................................ 148
Exam syllabus..................................................................... 149
Freely available mock exams on the Internet...................... 149
References.............................................................................. 151

You might also like