Transcription of The Java Tutorial - pearsoncmg.com
1 The java TutorialSixth i11/12/14 1:48 PM11/12/14 1:48 PMSince 1996, when Addison-Wesley published the first edition of The java Programming Language by Ken Arnold and James Gosling, this series has been the place to go for complete, expert, and definitive information on java technology. The books in this series provide the detailed information developers need to build effective, robust, and portable applications and are an indispensable resource for anyone using the java for a complete list of available java Series Make sure to connect with us! ii11/12/14 1:48 PM11/12/14 1:48 PMThe java TutorialA Short Course on the BasicsSixth EditionRaymond GallardoScott HommelSowmya KannanJoni GordonSharon Biocca ZakhourUpper Saddle River, NJ Boston Indianapolis San FranciscoNew York Toronto Montreal London Munich Paris MadridCapetown Sydney Tokyo Singapore Mexico iii11/12/14 1:48 PM11/12/14 1:48 PMMany of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks.
2 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 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. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained information about buying this title in bulk quantities, or for special sales opportunities (which may include electronic versions; custom cover designs.)
3 And content particular to your business, training goals, marketing focus, or branding interests), please contact our corporate sales department at or (800) 382- government sales inquiries, please contact For questions about sales outside the , please contact Visit us on the Web: of Congress Cataloging- in- Publication DataGallardo, java Tutorial : a short course on the basics / Raymond Gallardo, Scott Hommel, Sowmya Kannan,Joni Gordon, Sharon Biocca Zakhour. Sixth cmPrevious edition: The java Tutorial : a short course on the basics / Sharon Zakhour, Sowmya Kannan, Raymond Gallardo. 2013, which was originally based on The java Tutorial / by Mary 978- 0- 13- 403408- 9 (pbk.
4 : alk. paper) ISBN 0- 13- 403408- 2 (pbk. : alk. paper)1. java (Computer program language) I. '3 dc23 2014035811 Copyright 2015, Oracle and/or its affi liates. All rights Oracle Parkway, Redwood Shores, CA 94065 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.
5 , Permissions Department, One Lake Street, Upper Saddle River, New Jersey 07458, or you may fax your request to (201) 236- 13: 978- 0- 13- 403408- 9 ISBN- 10: 0- 13- 403408- 2 Text printed in the United States on recycled paper at Edwards Brothers Malloy in Ann Arbor, printing, December iv11/12/14 1:48 PM11/12/14 1:48 PMvPreface xxiiiAbout the Authors xxviiChapter 1 Getting Started 1 The java Technology Phenomenon 1 The java Programming Language 2 The java Platform 2 What Can java Technology Do? 4 How Will java Technology Change My Life? 4 The Hello World! Application 5 Hello World! for the NetBeans IDE 6 Hello World!
6 For Microsoft Windows 15 Hello World! for Solaris and Linux 20A Closer Look at the Hello World! Application 23 Source Code Comments 24 The HelloWorldApp Class Defi nition 25 The main Method 25 Common Problems (and Their Solutions) 27 Compiler Problems 27 Runtime Problems 29 Questions and Exercises: Getting Started v11/12/14 1:48 PM11/12/14 1:48 PMvi ContentsQuestions 31 Exercises 32 Answers 32 Chapter 2 Object- Oriented Programming Concepts 33 What Is an Object?
7 34 What Is a Class? 36 What Is Inheritance? 38 What Is an Interface? 39 What Is a Package? 40 Questions and Exercises: Object- Oriented Programming Concepts 41 Questions 41 Exercises 41 Answers 41 Chapter 3 Language Basics 43 Variables 44 Naming 45 Primitive Data Types 46 Arrays
8 51 Summary of Variables 57 Questions and Exercises: Variables 57 Operators 58 Assignment, Arithmetic, and Unary Operators 59 Equality, Relational, and Conditional Operators 62 Bitwise and Bit Shift Operators 65 Summary of Operators 66 Questions and Exercises: Operators 67 Expressions, Statements, and Blocks 68 Expressions 68 Statements 70 Blocks 71 Questions and Exercises.
9 Expressions, Statements, and Blocks 71 Control Flow Statements 72 The if- then and if- then- else Statements 72 The switch Statement 74 The while and do- while Statements 79 The for Statement 80 Branching Statements 82 Summary of Control Flow Statements 85 Questions and Exercises: Control Flow Statements vi11/12/14 1:48 PM11/12/14 1:48 PMContents viiChapter 4 Classes and Objects 87 Classes 88 Declaring Classes 89 Declaring Member Variables 90 Defi ning Methods 92 Providing Constructors for Your Classes 94 Passing Information to a Method or a Constructor 95 Objects 99 Creating Objects 100 Using Objects 104 More on Classes 107 Returning a Value from a Method 107 Using the this Keyword 109 Controlling Access to Members of a Class 110 Understanding Class
10 Members 112 Initializing Fields 116 Summary of Creating and Using Classes and Objects 118 Questions and Exercises: Classes 119 Questions and Exercises: Objects 120 Nested Classes 121 Why Use Nested Classes? 122 Static Nested Classes 122 Inner Classes 123 Shadowing 123 Serialization 124 Inner Class Example 125 Local and Anonymous Classes 127 Modifi ers 127 Local Classes 127 Anonymous Classes 131 Lambda Expressions 136 When to Use Nested Classes, Local Classes, Anonymous Classes, and Lambda Expressions 155 Questions and Exercises: Nested Classes 156 Enum Types 157 Questions and Exercises.