Java 2 Primer PlusAnnotation The dual purpose of this book is to provide a complete introduction to the Java programming language that balances theory with practicality. It will allow readers to (1) become Java certified programmers and (2) become productive in their Java development., and to give concrete experience with integrating Java programming technologies. Organized in an instructional style with review questions and projects. This book is based upon the new Java 1.4 platform. Steven Haines is both a practitioner and instructor. He uses the most recent examples and information from the technology industry to provide students with sound Java programming skills. The last part of the book shows how to integrate Java technologies into everyday Web related practices and with other technologies. Java 2 Primer Plusguides the reader through the Java programming language from the basic concepts (basic syntax, variable, literals, operators, loops, conditional statements, etc.) through advanced topics (RMI, JDBC, Network socket programming, etc.). It provides a deep explanation of Java programming concepts and technologies using classroom tested and proven techniques. Steven Hainesis currently the Product Manager for all J2EE products at Quest Software and part of the architectural team that defines the technical strategic direction of future products; he previously worked as the architect on a various range of J2EE products from a large scale B2B e-commerce application to a tight high volume product. He has taught Java at Learning Tree University, with topics ranging from beginning Java through advanced courses including JSP/Servlet-based Web development and Enterprise JavaBeans. In addition to publishing Java 2 From Scratch(Que) in late 1999, he writes an Enterprise Java column on InformIT.com. |
Contents
INTRODUCTION | 1 |
What You Need to Know Before Reading This Book | 2 |
Conventions Used In This Book | 3 |
Java Foundation | 5 |
Introduction to Java | 7 |
Why Use Java? | 8 |
Java Buzzwords | 9 |
Setting Up a Java Programming Environment | 11 |
The ComponentListener and ComponentAdapter Interfaces | 334 |
The MouseListener and MouseAdapter Interfaces | 336 |
The MouseMotionListener and MouseMotionAdapter Interfaces | 339 |
Summary | 341 |
Exercises | 342 |
JavaBeans | 343 |
Understanding JavaBeans | 344 |
Developing a Simple JavaBean | 346 |
Microsoft Windows | 12 |
Writing Your First Java Program | 13 |
Running Your First Java Program | 15 |
Summary | 19 |
Keywords Data Types and Variables | 21 |
Keywords | 22 |
The ComputerHuman Communication Problem | 23 |
Primitive Data Types | 24 |
Variables | 28 |
Literals | 30 |
Conversion Between Numeric Types | 31 |
Conversion Through Assignment | 32 |
Casting Between Data Types | 33 |
Summary | 34 |
Operators | 37 |
Subtraction | 38 |
Division | 39 |
Increment and Decrement Operators | 40 |
Relational Operators | 41 |
BitWise Operators | 42 |
AND | 43 |
OR | 44 |
NOT Operator | 45 |
Logical Operators | 46 |
ShortCircuit Operators | 47 |
Shift Left Operator | 49 |
Shift Right Fill with Os | 50 |
Java Operator Precedence | 51 |
Summary | 53 |
Flow Control | 55 |
The ifthenelse Statements | 56 |
The switch Statement | 59 |
Loop Constructs | 61 |
The while Statement | 62 |
The for Statement | 64 |
Breaking Out of Loops | 67 |
Labels | 68 |
Summary | 70 |
Review Questions | 71 |
Methods | 73 |
The Format of a Method | 74 |
Calling a Method | 75 |
Complex Class Methods | 77 |
Variable Scope | 78 |
Recursion | 80 |
Recursion Versus Iteration | 81 |
Java Class Libraries | 83 |
Summary | 85 |
Review Questions | 86 |
ObjectOriented Programming | 87 |
Classes | 89 |
ObjectOriented Programming | 96 |
Encapsulation | 97 |
Component Attributes | 100 |
State Attributes | 101 |
GetSet Methods | 102 |
Constructors | 103 |
Class Scope | 104 |
this Variable | 105 |
Composition | 106 |
Finalizers | 107 |
Static Class Members | 108 |
Access Modifiers | 109 |
Anonymous Inner Classes | 110 |
Understanding Object References | 111 |
Summary | 112 |
Exercises | 113 |
Inheritance | 115 |
Interfaces | 124 |
When to Use Inheritance | 133 |
Syntax | 134 |
Access Specifiers | 137 |
Dynamic Binding and Polymorphism | 138 |
Summary | 140 |
Exercises | 141 |
Interfaces | 143 |
Multiple Inheritance | 148 |
Interface Syntax | 149 |
Interfaces Used for Multiple Inheritance Reasons | 150 |
Interfaces Used to Add External Capabilities to a Class | 151 |
Review Questions | 152 |
Exception Handling | 153 |
Exception Definitions | 157 |
Finally Block | 158 |
Exception Classes | 160 |
Throwable Class | 161 |
Throwing Exceptions | 162 |
Creating an Instance of an Exception | 163 |
Rethrowing an Exception | 164 |
Summary | 171 |
Exercises | 172 |
Java Classes | 173 |
Using Javas Wrapper Classes | 175 |
The Boolean Class | 176 |
The Byte Class | 177 |
The Character Class | 179 |
The Double Class | 181 |
The Float Class | 183 |
The Integer Class | 185 |
The Long Class | 186 |
The Short Class | 188 |
Using String Classes | 190 |
The String Class | 191 |
The StringBuffer Class | 195 |
The StringTokenizer Class | 198 |
Summary | 199 |
Review Questions | 200 |
Collection Classes | 201 |
Introduction to Data Structures | 202 |
The Linked List Data Structure | 204 |
The Stack Data Structure | 206 |
The Queue Data Structure | 207 |
The Hash Table Data Structure | 208 |
The Tree Data Structure | 209 |
Collection Concepts | 212 |
Map Interface | 213 |
ArrayList | 214 |
Vector | 216 |
LinkedList | 219 |
The Stack Class | 221 |
HashSet | 222 |
TreeSet | 224 |
HashMap | 226 |
TreeMap | 228 |
Summary | 231 |
Using Javas Input and Output Classes | 233 |
Streams As Input and Output Data Sources | 234 |
ByteOriented Stream Classes | 235 |
Predefined Stream Objects | 237 |
Reading From and Writing to Files | 238 |
Other Byte IO Classes | 242 |
File IO with ReadersWriters | 244 |
Byte and Character IO Classes | 245 |
StreamTokenizer | 246 |
Chaining Streams For Ease of Use | 247 |
Exercises | 248 |
Graphical UserInterfaces | 249 |
Abstract Windows Toolkit | 251 |
Containers | 252 |
Applets | 253 |
Running Applets | 254 |
Frames | 256 |
Panels | 262 |
ScrollPane | 264 |
Programming with Dialogs | 267 |
Adding AWT Components to the GUI | 271 |
Adding Buttons | 274 |
TextArea | 277 |
CheckBox | 280 |
CheckboxGroup | 283 |
Programming with a Choice Control | 286 |
Programming with a List Control | 288 |
MenuBar | 291 |
MenuItem | 293 |
Layout Managers | 296 |
The BorderLayout Manager | 297 |
FlowLayout | 299 |
GridBagLayout | 301 |
Summary | 305 |
Exercises | 306 |
Event Delegation Model | 307 |
Understanding Events | 308 |
Programming with the Java Event Model | 309 |
Using the Other Event Listener Interfaces | 316 |
The ActionListener Interface | 318 |
The ItemListener Interface | 321 |
The TextListener Interface | 324 |
The KeyListener and KeyAdapter Interfaces | 327 |
The WindowListener and WindowAdapter Interfaces | 330 |
Deploying JavaBeans | 350 |
Deployment in Forte | 353 |
Properties Methods and Events | 357 |
Events | 359 |
Summary | 365 |
Exercises | 366 |
The Swing Library | 367 |
Understanding Swing | 368 |
Swing Application Structure | 369 |
Managing Windows | 372 |
JFrame | 374 |
JComponent | 375 |
Using the JScrollPane Control | 377 |
Dialogs | 380 |
Using Swing Components | 383 |
Adding Buttons | 385 |
JTextArea | 388 |
JCheckBox | 391 |
JRadioButton | 394 |
JList | 396 |
Summary | 399 |
Exercises | 400 |
Advanced Swing | 401 |
Adding Headers Scrolling and Formatting | 407 |
Programming with JTree | 411 |
Using the JOptionPane | 419 |
Summary | 421 |
Review Questions | 422 |
Advanced Topics | 423 |
Programming with Threads | 425 |
Controlling Threads with the Thread Class | 427 |
Creating Multithreaded Applications | 428 |
Setting the Names of Threads | 430 |
Creating Threads with the Runnable Interface | 432 |
Interrupting a Thread | 434 |
Stopping a Thread | 437 |
Coordinating Access to Variables from Threads | 440 |
Coordination Between Threads | 445 |
Sending Data Between Threads | 449 |
Summary | 453 |
Exercises | 454 |
Accessing Databases with Java Database Connectivity JDBC | 455 |
Understanding Database History | 456 |
Understanding the Need for JDBC | 457 |
Programming with Java Database Connectivity | 459 |
Querying the Database | 462 |
Creating Tables with JDBC | 470 |
Populating a Database | 473 |
Altering the Data Using JDBC and a GUI | 476 |
Summary | 484 |
Review Questions | 485 |
Network Programming | 487 |
TCPIP Internals | 488 |
The Application Layer | 489 |
The Transport Layer | 490 |
The Internetwork Layer | 491 |
Programming Simple Sockets | 492 |
TwoWay Communication Using Sockets | 497 |
Transferring Complex Objects with Sockets | 504 |
Transferring Streams with Sockets | 513 |
The URL Class | 518 |
Summary | 521 |
Exercises | 522 |
Web Technologies | 523 |
Servlets | 525 |
What Servlets Are and Are Not | 526 |
How Servlets Work | 527 |
Setting Up a Web Environment | 528 |
Installing Jakarta Tomcat | 529 |
Starting Tomcat | 530 |
Testing the Installation | 531 |
Programming Servlets | 532 |
Programming Generic Servlets | 533 |
Deploying the Servlet | 535 |
Understanding HTTP | 540 |
The GET Command | 541 |
The POST Command | 543 |
Programming HTTP | 544 |
HTML Forms and Servlets | 551 |
Calling Other Classes from Servlets | 557 |
Using Cookies to Maintain State | 563 |
Using Session Objects to Maintain State | 570 |
Summary | 578 |
Review Questions | 579 |
JavaServer Pages JSP | 581 |
What JSP Is and Is Not | 582 |
How JavaServer Pages Works | 583 |
Programming with JSP | 584 |
Adding Comments | 585 |
Deploying and Running JSP | 586 |
Defining Methods in JSP | 595 |
Conditional Output | 598 |
Calling JSP from HTML Forms | 599 |
Error Handling | 600 |
Summary | 602 |
Exercises | 603 |
ComponentBased JavaServer Pages | 605 |
Understanding JSP Actions | 607 |
How to include Other JSPs in Your JSP | 608 |
The include Directive | 609 |
The include Action | 610 |
Accessing JavaBeans from JSPs | 616 |
Using JavaBeans to Access Databases | 626 |
Updating the Database | 628 |
Retrieving the Data from the Database | 632 |
Summary | 642 |
Review Questions | 643 |
Custom Tag Libraries | 645 |
How Custom Tags Work | 646 |
Using the Custom Tag Body | 652 |
Passing Parameters with Custom Tags | 655 |
Summary | 666 |
XML | 667 |
XML Components | 670 |
Document Validation | 672 |
Reading XML Documents Using the Simple API for XML SAX Parser | 673 |
Manipulating XML Documents Using the Document Object Model DOM | 683 |
Reading from the XML Tree | 684 |
Outputting the XML Tree | 689 |
XSLT | 693 |
XSL Stylesheets | 695 |
An Example Using XSLT | 696 |
Manipulation XML Documents Using JDOM | 698 |
Reading an XML Document | 699 |
JDOM Example | 704 |
Summary | 707 |
Web Architecture | 709 |
Organizing a Web Site | 710 |
Hypertext Transport Protocol HTTP | 711 |
The Browser | 712 |
The Application | 713 |
The ModelViewController MVC Architecture | 714 |
Servlets | 715 |
JavaServer Pages JSP | 718 |
JavaScript | 720 |
Web Services | 722 |
Summary | 725 |
APPENDIXES | 727 |
Using Borland JBuilder | 729 |
Installing JBuilder | 730 |
Developing Java Applications with the IDE | 732 |
Debugging a Java Application | 735 |
Developing JSPs and Java Servlets with the IDE | 738 |
Summary | 743 |
Sun One Studio Forte Community Edition | 745 |
Installing Sun One Studio 4 | 746 |
Developing Java Applications with the IDE | 748 |
Debugging a Java Application | 751 |
Developing JSPs and Java Servlets with the IDE | 753 |
Adding a JSP Page | 754 |
Adding a Servlet | 757 |
Summary | 760 |
Answers to Review Questions | 761 |
Chapter 3 | 762 |
Chapter 4 | 763 |
Chapter 5 | 764 |
Chapter 7 | 765 |
Chapter 8 | 766 |
Chapter 10 | 767 |
Chapter 11 | 768 |
Chapter 12 | 769 |
Chapter 14 | 770 |
Chapter 17 | 771 |
Chapter 20 | 772 |
Chapter 23 | 773 |
Chapter 26 | 774 |
775 | |
Common terms and phrases
action added application args attributes body boolean button byte called catch chapter character client components connection constructor contains Continued create Current custom database defined display document double element event example exception execute extends false Figure final float Frame functionality handle implements import input instance Integer interface Java JavaBeans language Layer LISTING look loop method node Notice object operation output package parameter passed perform problem Project provides public class public static void public void reference remove represent request response result running Search server servlet shown shows simple specified speed statement static void main(String stream String System.out.println Table thread throws tree true variable window write