Transcription of Core Java™ Volume I—-Fundamentals
1 Core Java . Volume I Fundamentals Ninth Edition The Core Series PRENTICE. HALL. Visit for a complete list of available publications. The Core Series is designed to provide you - the experienced programmer - with the essential information you need to quickly learn and apply the latest, most important technologies. Authors in The Core Series are seasoned professionals who have pioneered the use of these technologies to achieve tangible results in rea -world settings. These experts: Share their practical experiences Support their instruction with rea -world examples Provide an accelerated, highly effective path to learning the subject at hand The resulting book is a no-nonsense tutorial and thorough reference that allows you to quickly produce robust, production-quality code.
2 Make sure to connect with us! Safari ALWAYS LEARNING PEARSON. Core Java . Volume I Fundamentals Ninth Edition Cay S. Horstmann Gary Cornell Upper Saddle River, NJ Boston Indianapolis San Francisco New York Toronto Montreal London Munich Paris Madrid Capetown Sydney Tokyo Singapore Mexico City Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals. The authors and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions.
3 No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein. The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales, which may include electronic versions and/or custom covers and content particular to your business, training goals, marketing focus, and branding interests. For more information, please contact: Corporate and Government Sales (800) 382-3419. For sales outside the United States, please contact: International Sales Visit us on the Web: Library of Congress Cataloging-in-Publication Data Horstmann, Cay S., 1959- Core Java / Cay S. Horstmann, Gary Cornell. Ninth edition.
4 Pages cm Includes index. ISBN 978-0-13-708189-9 (v. 1 : pbk. : alk. paper) 1. Java (Computer program language) I. Cornell, Gary. II. Title. 2013. '3 dc23. 2012035397. Copyright 2013 Oracle and/or its affiliates. All rights reserved. 500 Oracle Parkway, Redwood Shores, CA 94065. All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. To obtain permission to use material from this work, please submit a written request to Pearson Education, Inc., Permissions Department, One Lake Street, Upper Saddle River, New Jersey 07458, or you may fax your request to (201) 236-3290.
5 ISBN-13: 978-0-13-708189-9. ISBN-10: 0-13-708189-8. Text printed in the United States on recycled paper at Edwards Brothers Malloy in Ann Arbor, Michigan. Second printing, July 2013. Contents Preface .. xix Acknowledgments .. xxv Chapter 1: An Introduction to Java .. 1. Java As a Programming Platform .. 1. The Java White Paper Buzzwords .. 2. Simple .. 3. Object-Oriented .. 4. Network-Savvy .. 4. Robust .. 5. Secure .. 5. Architecture-Neutral .. 6. Portable .. 7. Interpreted .. 7. High-Performance .. 8. Multithreaded .. 8. Dynamic .. 8. Java Applets and the Internet .. 9. A Short History of Java .. 10. Common Misconceptions about Java .. 13. Chapter 2: The Java Programming Environment .. 17. Installing the Java Development Kit.
6 18. Downloading the JDK .. 18. Setting the Executable Path .. 20. Installing the Library Source and Documentation .. 22. Installing the Core Java Program Examples .. 23. Navigating the Java Directories .. 24. Choosing a Development Environment .. 24. Using the Command-Line Tools .. 25. v vi Contents Troubleshooting Hints .. 27. Using an Integrated Development Environment .. 28. Locating Compilation Errors .. 32. Running a Graphical Application .. 33. Building and Running Applets .. 36. Chapter 3: Fundamental Programming Structures in Java .. 41. A Simple Java Program .. 42. Comments .. 45. Data Types .. 46. Integer Types .. 47. Floating-Point Types .. 48. The char Type .. 49. The boolean Type .. 51. Variables .. 52. Initializing Variables.
7 53. Constants .. 54. Operators .. 55. Increment and Decrement Operators .. 56. Relational and boolean Operators .. 57. Bitwise Operators .. 58. Mathematical Functions and Constants .. 59. Conversions between Numeric Types .. 60. Casts .. 61. Parentheses and Operator Hierarchy .. 62. Enumerated Types .. 63. Strings .. 64. Substrings .. 64. Concatenation .. 64. Strings Are Immutable .. 65. Testing Strings for Equality .. 67. Empty and Null Strings .. 68. Code Points and Code Units .. 68. The String API .. 69. Reading the Online API Documentation .. 72. Building Strings .. 74. Contents vii Input and Output .. 76. Reading Input .. 76. Formatting Output .. 79. File Input and Output .. 84. Control Flow .. 86. Block Scope .. 86. Conditional Statements.
8 87. Loops .. 91. Determinate Loops .. 95. Multiple Selections The switch Statement .. 99. Statements That Break Control Flow .. 102. Big Numbers .. 105. Arrays .. 107. The for each Loop .. 109. Array Initializers and Anonymous Arrays .. 110. Array Copying .. 111. Command-Line Parameters .. 112. Array Sorting .. 113. Multidimensional Arrays .. 116. Ragged Arrays .. 120. Chapter 4: Objects and Classes .. 125. Introduction to Object-Oriented Programming .. 126. Classes .. 127. Objects .. 128. Identifying Classes .. 129. Relationships between Classes .. 129. Using Prede ned Classes .. 132. Objects and Object Variables .. 132. The GregorianCalendar Class of the Java Library .. 136. Mutator and Accessor Methods .. 138. De ning Your Own Classes.
9 145. An Employee Class .. 145. Use of Multiple Source Files .. 148. Dissecting the Employee Class .. 149. First Steps with Constructors .. 150. viii Contents Implicit and Explicit Parameters .. 152. Bene ts of Encapsulation .. 153. Class-Based Access Privileges .. 156. Private Methods .. 156. Final Instance Fields .. 157. Static Fields and Methods .. 157. Static Fields .. 157. Static Constants .. 158. Static Methods .. 159. Factory Methods .. 161. The main Method .. 161. Method Parameters .. 164. Object Construction .. 171. Overloading .. 171. Default Field Initialization .. 172. The Constructor with No Arguments .. 173. Explicit Field Initialization .. 174. Parameter Names .. 175. Calling Another Constructor .. 176. Initialization Blocks.
10 176. Object Destruction and the finalize Method .. 181. Packages .. 182. Class Importation .. 182. Static Imports .. 184. Addition of a Class into a Package .. 185. Package Scope .. 188. The Class Path .. 190. Setting the Class Path .. 192. Documentation Comments .. 193. Comment Insertion .. 193. Class Comments .. 194. Method Comments .. 195. Field Comments .. 196. General Comments .. 196. Package and Overview Comments .. 197. Contents ix Comment Extraction .. 198. Class Design Hints .. 199. Chapter 5: Inheritance .. 203. Classes, Superclasses, and Subclasses .. 204. Inheritance Hierarchies .. 212. Polymorphism .. 213. Dynamic Binding .. 214. Preventing Inheritance: Final Classes and Methods .. 217. Casting .. 218. Abstract Classes.