[PDF] Java The Complete Reference Seventh Edition





Previous PDF Next PDF



ADVANCED JAVA PROGRAMS PRACTICAL 1

ADVANCED JAVA PROGRAMS. PRACTICAL 1. Objective. Write a program to create a frame using AWT. Implement mouseClicked() mouseEntered() and. mouseExited() events 



Advanced Java Programming Lab (7MCE1P1)

To write a java program to create sample application form in JApplet using swing control. Algorithm: Step 1: Define class pgm8 which extends from JApplet and 



Advanced-java.pdf

For example the following code does not compile in Java 7: public static < T > void performAction( final Collection< T > actions



Java Code Conventions

4 окт. 1996 г. Files longer than 2000 lines are cumbersome and should be avoided. For an example of a Java program properly formatted see “Java Source File ...



Teach Yourself Java in 21 Days

For example a printing code of 96-1 shows that the first printing of the advanced topic covered better in books on C or C++. Table. 3.6 summarizes the ...



Teaching Guidelines for Web-based Java Programming PG-DAC

Objective: To learn advanced concepts in java programming and perform web Programming using Java. • RESTful web service JSON example. • RESTful web service ...



Programming in Java Advanced Imaging

6 мая 2011 г. ... Java Advanced Imaging. API is extended. Appendix A “Program Examples



Authoring Worked Examples for Java Programming with Human-AI

4 дек. 2023 г. an advanced Java programming class. Graduate students selected for the study usually serve as assistants or instructors in program- ming ...



onbot-java-guide.pdf

You can see from the sample code that an op mode is defined as a Java class. In this example the op mode name is called “MyFIRSTJavaOpMode” and it inherits 



Evaluating and Securing Text-Based Java Code through Static

3 июн. 2020 г. These book chapters use modern examples of advanced Java concepts being taught for CS2 and. Data structures courses. The source code data was ...



ADVANCED JAVA PROGRAMS PRACTICAL 1

ADVANCED JAVA PROGRAMS. PRACTICAL 1. Objective. Write a program to create a frame using AWT. Implement mouseClicked() mouseEntered() and.



Advanced-java.pdf

Java programming language originated in Sun Microsystems and released back in 1995



TutorialsPoint

advanced concepts related to Java Programming language. Prerequisites. Before you start practicing various types of examples given in this reference 



Teach Yourself Java in 21 Days

For example a printing code of 96-1 shows that the with more advanced concepts in putting together Java programs and working with the standard.



Advanced Java Programming Lab (7MCE1P1)

To write a java program to create sample application form in JApplet using swing control. Algorithm: Step 1: Define class pgm8 which extends from JApplet and 



Programming in Java Advanced Imaging

06-May-2011 use Java Advanced Imaging for real projects. To best understand this document and the examples you need a solid background in the Java ...



Java The Complete Reference Seventh Edition

A Closer Look at the First Sample Program . Another Enumeration Example . ... Object-oriented programming (OOP) is at the core of Java.



Advanced Java Programming with Database Application

A brief definition might be: THE INFORMATION HELD OVER A PERIOD OF TIME



Advance Java Multiple Choice Questions With Answers [PDF] - m

16-Jun-2022 Includes examples of actual web based experiments. 800+ MCQs on Advanced Java Programming. Yogita Jore 2020-06-23 "800+ MCQ on Advanced.



Advanced Java Programming (A0510125) - Applets Unit-I

Simple Example: Applets differ from console-based applications in several key areas. import java.awt.*; import java.applet 

Java

The Complete Reference,

Seventh Edition

About the Author

Herbert Schildtis a leading authority on the

Java, C, C++, and C# languages, and is a master

Windows programmer. His programming books

have sold more than 3.5 million copies worldwide and have been translated into all major foreign languages. He is the author of the best-selling

The Art of Java, Java: A Beginner"s Guide,and

Swing: A Beginner"s Guide. Among his other

bestsellers areC++: The Complete Reference, C++: A Beginner"s Guide, C#: The Complete Reference,and C#: A Beginner"s Guide. Schildt holds both graduate and undergraduate degrees from the University of Illinois. He can be reached at his consulting office at (217) 586-4683. His Web site is www.HerbSchildt.com. Java

The Complete Reference,

Seventh Edition

Herbert Schildt

New York Chicago San Francisco

Lisbon London Madrid Mexico City

Milan New Delhi San Juan

Seoul Singapore Sydney Toronto

Copyright © 2007 by The McGraw-Hill Companies. All rights reserved. Except as permitted under the United States Copyrig

ht Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the publisher.

ISBN: 978-0-07-163177-8

MHID: 0-07-163177-1

The material in this eBook also appears in the print version of this tit le: ISBN: 978-0-07-226385-5,

MHID: 0-07-226385-7.

All trademarks are trademarks of their respective owners. Rather than pu t a trademark symbol after every occurrence of a trademarked name,

we use names in an editorial fashion only, and to the benefi t of the trademark owner, with no intention of infringement of the trademark. Where

such designations appear in this book, they have been printed with initi al caps. McGraw-Hill eBooks are available at special quantity discounts to use as premiums and sales promotions, or for use in corporate training programs. To contact a representative please e-mail us at bulksales@mcgraw-hill.com Information has been obtained by McGraw-Hill from sources believed to be reliable. However, because of the possibility of human or mechanical error by our sources, McGraw-Hill, or others, McGraw-Hill doe s not guarantee the accuracy, adequacy, or completeness of any information and is not responsible for any errors or omissions or the re sults obtained from the use of such information.

TERMS OF USE

This is a copyrighted work and The McGraw-Hill Companies, Inc. ("McGrawHill") and its licensors reserve all rights in and to the work. Use of this work is subject to these terms. Except as permitted under the Co pyright Act of 1976 and the right to store and retrieve one copy of the

work, you may not decompile, disassemble, reverse engineer, reproduce, modify, create derivative works based upon, transmit, distribute,

disseminate, sell, publish or sublicense the work or any part of it with out McGraw-Hill's prior consent. You may use the work for your own noncommercial and personal use; any other use of the work is strictly pr ohibited. Your right to use the work may be terminated if you fail to comply with these terms. THE WORK IS PROVIDED "AS IS." McGRAW-HILL AND ITS LICENSORS MAKE NO GUARANTEES OR WARRANTIES AS TO THE ACCURACY, ADEQUACY OR COMPLETENESS OF OR RESULTS TO BE OBTAINED FROM USING THE WORK, INCLUDING ANY INFORMATION THAT CAN BE ACCESSED THROUGH THE WORK VIA HYPERLINK OR OTHERWISE, AND

EXPRESSLY DISCLAIM ANY WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES OF

MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. McGraw-Hill and its licensors do not warrant or guarant

ee that the functions contained in the work will meet your requirements or that its operation will be uninterrupted or error free. Neither McGraw-Hill nor

its licensors shall be liable to you or anyone else for any inaccuracy, error or omission, regardless of cause, in the work or for any damages

resulting therefrom. McGraw-Hill has no responsibility for the content o f any information accessed through the work. Under no circumstances shall McGraw-Hill and/or its licensors be liable for any indirect, incid ental, special, punitive, consequential or similar damages that result from the use of or inability to use the work, even if any of them has been ad vised of the possibility of such damages. This limitation of liability shall apply to any claim or cause whatsoever whether such claim or cause arise s in contract, tort or otherwise.

Contents at a Glance

Part IThe Java Language

1The History and Evolution of Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

2An Overview of Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

3Data Types, Variables, and Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33

4Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57

5Control Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77

6Introducing Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105

7ACloser Look at Methods and Classes . . . . . . . . . . . . . . . . . . . . . . . . 125

8Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157

9Packages and Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183

10Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205

11Multithreaded Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223

12Enumerations, Autoboxing, and Annotations (Metadata) . . . . . . . . . 255

13I/O, Applets, and Other Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285

14Generics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315

Part IIThe Java Library

15String Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359

16Exploring java.lang . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385

17java.util Part 1: The Collections Framework . . . . . . . . . . . . . . . . . . . . 437

18java.util Part 2: More Utility Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . 503

19Input/Output: Exploring java.io . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 555

20Networking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 599

21The Applet Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 617

22Event Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 637

23Introducing the AWT: Working with Windows, Graphics, and Text . . 663

24Using AWT Controls, Layout Managers, and Menus . . . . . . . . . . . . 701

25Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 755

26The Concurrency Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 787

27NIO, Regular Expressions, and Other Packages . . . . . . . . . . . . . . . . . 813

v

Part IIISoftware Development Using Java

28Java Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 847

29Introducing Swing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 859

30Exploring Swing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 879

31Servlets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 907

Part IVApplying Java

32Financial Applets and Servlets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 931

33Creating a Download Manager in Java . . . . . . . . . . . . . . . . . . . . . . . . . 965

AUsing Java"s Documentation Comments . . . . . . . . . . . . . . . . . . . . . . . 991

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 997

viJava: The Complete Reference

Contents

Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxix

Part IThe Java Language

1The History and Evolution of Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

Java"s Lineage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

The Birth of Modern Programming: C . . . . . . . . . . . . . . . . . . . 4 C++: The Next Step . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 The Stage Is Set for Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

The Creation of Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

The C# Connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 How Java Changed the Internet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Java Applets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Portability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Java"s Magic: The Bytecode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Servlets: Java on the Server Side . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

The Java Buzzwords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

Simple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

Object-Oriented . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

Robust . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

Multithreaded . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Architecture-Neutral . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Interpreted and High Performance . . . . . . . . . . . . . . . . . . . . . . 12 Distributed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Dynamic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

The Evolution of Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

Java SE 6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

ACulture of Innovation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

2An Overview of Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

Object-Oriented Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Two Paradigms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Abstraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 The Three OOP Principles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 AFirst Simple Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Entering the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Compiling the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 ACloser Look at the First Sample Program . . . . . . . . . . . . . . . 22 vii ASecond Short Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Two Control Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 The if Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 The for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

Using Blocks of Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

Lexical Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30

Whitespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30

Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30

Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31

Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Separators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 The Java Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31

The Java Class Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32

3Data Types, Variables, and Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33

Java Is a Strongly Typed Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33

The Primitive Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33

Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34

byte . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

short . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

int . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

long . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

Floating-Point Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36

float . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36

double . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36

Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37

Booleans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38

ACloser Look at Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39

Integer Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Floating-Point Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Boolean Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Character Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 String Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40

Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41

Declaring a Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Dynamic Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 The Scope and Lifetime of Variables . . . . . . . . . . . . . . . . . . . . . 42 Type Conversion and Casting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Java"s Automatic Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Casting Incompatible Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Automatic Type Promotion in Expressions . . . . . . . . . . . . . . . . . . . . . 47 The Type Promotion Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47

Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48

One-Dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Alternative Array Declaration Syntax . . . . . . . . . . . . . . . . . . . . 55 viiiJava: The Complete Reference AFew Words About Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 ANote to C/C++ Programmers About Pointers . . . . . . . . . . . . . . . . . 56

4Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57

Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57

The Basic Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . 58 The Modulus Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Arithmetic Compound Assignment Operators . . . . . . . . . . . . 59 Increment and Decrement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60

The Bitwise Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62

The Bitwise Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . 63 The Left Shift . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 The Right Shift . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 The Unsigned Right Shift . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 Bitwise Operator Compound Assignments . . . . . . . . . . . . . . . 69

Relational Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70

Boolean Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Short-Circuit Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . 72 The Assignment Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73

The ? Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73

Operator Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74

Using Parentheses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74

5Control Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77

Java"s Selection Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77

if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77

switch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80

Iteration Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84

while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84

do-while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86

for . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88

The For-Each Version of the for Loop . . . . . . . . . . . . . . . . . . . . 92 Nested Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97

Jump Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98

Using break . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 Using continue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102

return . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103

6Introducing Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105

Class Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105

The General Form of a Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 ASimple Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106

Declaring Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109

ACloser Look at new . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 Assigning Object Reference Variables . . . . . . . . . . . . . . . . . . . . . . . . . . 111

Introducing Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111

Adding a Method to the Box Class . . . . . . . . . . . . . . . . . . . . . . 112

Contentsix

Returning a Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 Adding a Method That Takes Parameters . . . . . . . . . . . . . . . . 115

Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117

Parameterized Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119

The this Keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120

Instance Variable Hiding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121

Garbage Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121

The finalize( ) Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121

AStack Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122

7A Closer Look at Methods and Classes . . . . . . . . . . . . . . . . . . . . . . . . 125

Overloading Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125

Overloading Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 Using Objects as Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 ACloser Look at Argument Passing . . . . . . . . . . . . . . . . . . . . . . . . . . . 132

Returning Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134

Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135

Introducing Access Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138

Understanding static . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141

Introducing final . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143

Arrays Revisited . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143

Introducing Nested and Inner Classes . . . . . . . . . . . . . . . . . . . . . . . . . 145

Exploring the String Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148

Using Command-Line Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 Varargs: Variable-Length Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . 151 Overloading Vararg Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 Varargs and Ambiguity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155

8Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157

Inheritance Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157

Member Access and Inheritance . . . . . . . . . . . . . . . . . . . . . . . . 159 AMore Practical Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 ASuperclass Variable Can Reference a Subclass Object . . . . . 162

Using super . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163

Using super to Call Superclass Constructors . . . . . . . . . . . . . . 163 ASecond Use for super . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 Creating a Multilevel Hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 When Constructors Are Called . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170

Method Overriding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171

Dynamic Method Dispatch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 Why Overridden Methods? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 Applying Method Overriding . . . . . . . . . . . . . . . . . . . . . . . . . . 176

Using Abstract Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177

Using final with Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180 Using final to Prevent Overriding . . . . . . . . . . . . . . . . . . . . . . . 180 Using final to Prevent Inheritance . . . . . . . . . . . . . . . . . . . . . . . 181

The Object Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181

xJava: The Complete Reference

9Packages and Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183

Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183

Defining a Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 Finding Packages and CLASSPATH . . . . . . . . . . . . . . . . . . . . . 184 AShort Package Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185

Access Protection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186

An Access Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187

Importing Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190

Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192

Defining an Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 Implementing Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194 Nested Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 Applying Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 Variables in Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 Interfaces Can Be Extended . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202

10Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205

Exception-Handling Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . 205

Exception Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206

Uncaught Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206

Using try and catch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207

Displaying a Description of an Exception . . . . . . . . . . . . . . . . 209

Multiple catch Clauses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209

Nested try Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211

throw . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213

throws . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214

finally . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216

Java"s Built-in Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217

Creating Your Own Exception Subclasses . . . . . . . . . . . . . . . . . . . . . . 219

Chained Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221

Using Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222

11Multithreaded Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223

The Java Thread Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224

Thread Priorities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224 Synchronization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225 Messaging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225 The Thread Class and the Runnable Interface . . . . . . . . . . . . . 226

The Main Thread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226

Creating a Thread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228

Implementing Runnable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228 Extending Thread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230 Choosing an Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232 Creating Multiple Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232

Using isAlive( ) and join( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233

Thread Priorities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236

Contentsxi

Synchronization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238

Using Synchronized Methods . . . . . . . . . . . . . . . . . . . . . . . . . . 239 The synchronized Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241 Interthread Communication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242 Deadlock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247 Suspending, Resuming, and Stopping Threads . . . . . . . . . . . . . . . . . . 249

Suspending, Resuming, and Stopping Threads

Using Java 1.1 and Earlier . . . . . . . . . . . . . . . . . . . . . . . . . . . 249

The Modern Way of Suspending, Resuming,

and Stopping Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251

Using Multithreading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254

12Enumerations, Autoboxing, and Annotations (Metadata) . . . . . . . . 255

Enumerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255

Enumeration Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 The values( ) and valueOf( ) Methods . . . . . . . . . . . . . . . . . . . . 258 Java Enumerations Are Class Types . . . . . . . . . . . . . . . . . . . . . 259 Enumerations Inherit Enum . . . . . . . . . . . . . . . . . . . . . . . . . . . 261 Another Enumeration Example . . . . . . . . . . . . . . . . . . . . . . . . . 263

Type Wrappers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264

quotesdbs_dbs17.pdfusesText_23
[PDF] advanced java programs examples with output pdf

[PDF] advanced java programs examples with output pdf free download

[PDF] advanced java subject code

[PDF] advanced java tutorial pdf tutorialspoint

[PDF] advanced javascript syllabus pdf

[PDF] advanced machine design nptel

[PDF] advanced machine design syllabus

[PDF] advanced microsoft access 2016 tutorial

[PDF] advanced microsoft access 2016 tutorial pdf

[PDF] advanced microsoft excel notes pdf

[PDF] advanced microsoft powerpoint 2007 tutorial pdf

[PDF] advanced ms excel 2016 tutorial pdf

[PDF] advanced ms excel book pdf

[PDF] advanced ms excel notes pdf

[PDF] advanced ms excel pdf download