getting started with java 1997.pdf

(1295 KB) Pobierz
Getting Started with Java
Getting Started with Java
VERSION 5
Borland ®
JBuilder
Borland Software Corporation
100 Enterprise Way, Scotts Valley, CA 95066-3249
www.borland.com
22942416.002.png
Refer to the file DEPLOY.TXT located in the redist directory of your JBuilder product for a complete list of files that
you can distribute in accordance with the JBuilder License Statement and Limited Warranty.
Borland Software Corporation may have patents and/or pending patent applications covering subject matter in this
document. The furnishing of this document does not give you any license to these patents.
C OPYRIGHT © 1997, 2001 Borland Software Corporation. All rights reserved. All Borland brand and product names
are trademarks or registered trademarks of Borland Software Corporation in the United States and other countries.
Other product names are trademarks or registered trademarks of their respective holders.
For third-party conditions and disclaimers, see the Release Notes on your JBuilder product CD.
Printed in the U.S.A.
JBE0050WW21000gsjava 2E1R0401
0102030405-9 8 7 6 54321
PDF
22942416.003.png
Contents
Chapter 1
Introduction 1-1
Documentation conventions . . . . . . . . . . . 1-3
Contacting Borland developer support . . . . . 1-4
Online resources . . . . . . . . . . . . . . . . 1-5
World Wide Web . . . . . . . . . . . . . . 1-5
Borland newsgroups . . . . . . . . . . . . 1-5
Usenet newsgroups. . . . . . . . . . . . . 1-5
Reporting bugs . . . . . . . . . . . . . . . 1-6
Chapter 2
Java language elements 2-1
Terms . . . . . . . . . . . . . . . . . . . . . . . . 2-1
Identifier. . . . . . . . . . . . . . . . . . . . . 2-1
Data type . . . . . . . . . . . . . . . . . . . . 2-2
Primitive data types . . . . . . . . . . . . 2-2
Composite data types . . . . . . . . . . . 2-3
Strings . . . . . . . . . . . . . . . . . . . . . . 2-3
Arrays . . . . . . . . . . . . . . . . . . . . . . 2-4
Variable . . . . . . . . . . . . . . . . . . . . . 2-4
Literal . . . . . . . . . . . . . . . . . . . . . . 2-4
Applying concepts . . . . . . . . . . . . . . . . . 2-5
Declaring variables . . . . . . . . . . . . . . . 2-5
Methods . . . . . . . . . . . . . . . . . . . . . 2-5
Chapter 3
Java language structure 3-1
Terms . . . . . . . . . . . . . . . . . . . . . . . . 3-1
Keywords . . . . . . . . . . . . . . . . . . . . 3-1
Operators . . . . . . . . . . . . . . . . . . . . 3-2
Comments. . . . . . . . . . . . . . . . . . . . 3-3
Statements. . . . . . . . . . . . . . . . . . . . 3-5
Code blocks . . . . . . . . . . . . . . . . . . . 3-5
Understanding scope . . . . . . . . . . . . . 3-5
Applying concepts . . . . . . . . . . . . . . . . . 3-6
Using operators. . . . . . . . . . . . . . . . . 3-6
Arithmetic operators . . . . . . . . . . . . 3-7
Logical operators . . . . . . . . . . . . . . 3-8
Assignment operators . . . . . . . . . . . 3-9
Comparison operators . . . . . . . . . . . 3-10
Bitwise operators . . . . . . . . . . . . . . 3-10
?:—The ternary operator . . . . . . . . . . 3-12
Using methods . . . . . . . . . . . . . . . . . 3-12
Using arrays. . . . . . . . . . . . . . . . . . . 3-13
Using constructors . . . . . . . . . . . . . . . 3-13
Member access . . . . . . . . . . . . . . . . . 3-14
Arrays . . . . . . . . . . . . . . . . . . . . 3-15
Chapter 4
Java language control 4-1
Terms . . . . . . . . . . . . . . . . . . . . . . . . . 4-1
String handling . . . . . . . . . . . . . . . . . 4-1
Type casting and conversion . . . . . . . . . . 4-2
Return types and statements . . . . . . . . . . 4-3
Flow control statements . . . . . . . . . . . . 4-3
Applying concepts . . . . . . . . . . . . . . . . . 4-4
Escape sequences . . . . . . . . . . . . . . . . 4-4
String handling. . . . . . . . . . . . . . . . 4-4
Determining access . . . . . . . . . . . . . . . 4-6
Handling methods . . . . . . . . . . . . . . . 4-7
Using type conversions . . . . . . . . . . . . . 4-8
Implicit casting . . . . . . . . . . . . . . . . 4-8
Explicit conversion . . . . . . . . . . . . . 4-8
Flow control . . . . . . . . . . . . . . . . . . . 4-9
Loops . . . . . . . . . . . . . . . . . . . . . 4-9
Loop control statements. . . . . . . . . . 4-11
Conditional statements . . . . . . . . . . . . 4-12
Handling exceptions . . . . . . . . . . . . . 4-13
Chapter 5
The Java class libraries 5-1
Java 2 Platform editions . . . . . . . . . . . . . . 5-1
Standard Edition. . . . . . . . . . . . . . . . . 5-2
Enterprise Edition . . . . . . . . . . . . . . . . 5-2
Micro Edition . . . . . . . . . . . . . . . . . . 5-2
Java 2 Standard Edition packages . . . . . . . . . 5-3
The Language package: java.lang . . . . . . . 5-4
The Utility package: java.util. . . . . . . . . . 5-4
The I/O package: java.io . . . . . . . . . . . . 5-4
The Text package: java.text . . . . . . . . . . . 5-4
The Math package: java.math . . . . . . . . . 5-5
The AWT package: java.awt . . . . . . . . . . 5-5
The Swing package: javax.swing . . . . . . . 5-5
The Javax packages: javax . . . . . . . . . . . 5-6
The Applet package: java.applet. . . . . . . . 5-6
The Beans package: java.beans. . . . . . . . . 5-7
The Reflection package: java.lang.reflect . . . 5-7
The SQL package: java.sql . . . . . . . . . . . 5-7
The RMI package: java.rmi . . . . . . . . . . . 5-8
The Networking package: java.net . . . . . . 5-8
The Security package: java.security . . . . . . 5-8
i
22942416.004.png
Chapter 6
Object-oriented programming
in Java 6-1
Classes . . . . . . . . . . . . . . . . . . . . . . . 6-2
Declaring and instantiating classes . . . . . . 6-2
Data members . . . . . . . . . . . . . . . . . 6-3
Class methods . . . . . . . . . . . . . . . . . 6-3
Constructors and finalizers . . . . . . . . . . 6-4
Case study: A simple OOP example . . . . . 6-4
Class inheritance . . . . . . . . . . . . . . . . 6-9
Calling the parent’s constructor . . . . . . 6-11
Access modifiers . . . . . . . . . . . . . . . . 6-11
Access from within class’s package . . . . 6-12
Access outside of a package . . . . . . . . 6-12
Accessor methods . . . . . . . . . . . . . . . 6-13
Abstract classes . . . . . . . . . . . . . . . . . 6-16
Polymorphism . . . . . . . . . . . . . . . . . . . 6-17
Using interfaces. . . . . . . . . . . . . . . . . 6-17
Adding two new buttons . . . . . . . . . . . 6-21
Running your application . . . . . . . . . . . 6-23
Java packages. . . . . . . . . . . . . . . . . . . . 6-23
The import statement . . . . . . . . . . . . . 6-23
Declaring packages. . . . . . . . . . . . . . . 6-24
Chapter 9
An introduction to the Java Virtual
Machine 9-1
Java VM security . . . . . . . . . . . . . . . . . . 9-3
The security model . . . . . . . . . . . . . . . 9-3
The Java verifier . . . . . . . . . . . . . . . 9-3
The Security Manager and the
java.security Package . . . . . . . . . . . 9-4
The class loader . . . . . . . . . . . . . . . 9-6
What about Just-In-Time compilers? . . . . . 9-6
Chapter 10
Working with the Java Native
Interface (JNI) 10-1
How JNI works . . . . . . . . . . . . . . . . . . 10-2
Using the native keyword . . . . . . . . . . . . 10-2
Using the javah tool. . . . . . . . . . . . . . . . 10-3
Chapter 11
Java language quick reference 11-1
Java 2 platform editions . . . . . . . . . . . . . 11-1
Java class libraries. . . . . . . . . . . . . . . . . 11-2
Keywords . . . . . . . . . . . . . . . . . . . . . 11-3
Data and return types and terms . . . . . . 11-3
Packages, classes, members, and
interfaces . . . . . . . . . . . . . . . . . . . 11-3
Access modifiers. . . . . . . . . . . . . . . . 11-4
Loops and loop controls . . . . . . . . . . . 11-4
Exception handling . . . . . . . . . . . . . . 11-5
Reserved . . . . . . . . . . . . . . . . . . . . 11-5
Converting and casting data types . . . . . . . 11-5
Primitive to primitive . . . . . . . . . . . . . 11-6
Primitive to String . . . . . . . . . . . . . . . 11-7
Primitive to reference . . . . . . . . . . . . . 11-8
String to primitive . . . . . . . . . . . . . . .11-10
Reference to primitive . . . . . . . . . . . .11-12
Reference to reference. . . . . . . . . . . . .11-13
Escape sequences . . . . . . . . . . . . . . . . .11-18
Operators . . . . . . . . . . . . . . . . . . . . .11-18
Basic operators. . . . . . . . . . . . . . . . .11-18
Arithmetic operators . . . . . . . . . . . . .11-19
Logical operators . . . . . . . . . . . . . . .11-20
Assignment operators. . . . . . . . . . . . .11-20
Comparison operators . . . . . . . . . . . .11-21
Bitwise operators . . . . . . . . . . . . . . .11-21
Ternary operator. . . . . . . . . . . . . . . .11-22
Chapter 7
Threading techniques 7-1
The lifecycle of a thread . . . . . . . . . . . . . . 7-2
Customizing the run() method . . . . . . . . 7-2
Subclassing the Thread class . . . . . . . 7-2
Implementing the Runnable interface . . 7-3
Defining a thread . . . . . . . . . . . . . . . . 7-5
Starting a thread . . . . . . . . . . . . . . . . 7-6
Making a thread not runnable . . . . . . . . 7-6
Stopping a thread. . . . . . . . . . . . . . . . 7-7
Thread priority . . . . . . . . . . . . . . . . . . . 7-7
Time slicing . . . . . . . . . . . . . . . . . . . 7-7
Synchronizing threads. . . . . . . . . . . . . . . 7-8
Thread groups . . . . . . . . . . . . . . . . . . . 7-8
Chapter 8
Serialization 8-1
Why serialize? . . . . . . . . . . . . . . . . . . . 8-1
Java serialization . . . . . . . . . . . . . . . . . . 8-2
Using the Serializable interface . . . . . . . . 8-2
Using output streams . . . . . . . . . . . . . . . 8-4
ObjectOutputStream methods . . . . . . . . 8-5
Using input streams . . . . . . . . . . . . . . . . 8-6
ObjectInputStream methods . . . . . . . . . 8-8
Writing and reading object streams . . . . . . . 8-8
Index
I-1
ii
22942416.005.png
1
Chapter1
Introduction
Java is an object-oriented programming language. Switching to
object-oriented programming (OOP) from other programming paradigms
can be difficult. Java focuses on creating objects (data structures or
behaviors) that can be assessed and manipulated by the program.
Like other programming languages, Java provides support for reading
and writing data to and from different input and output devices. Java uses
processes that increase the efficiency of input/output, facilitate
internationalization, and provide better support for non-UNIX platforms.
Java looks over your program as it runs and automatically deallocates
memory that is no longer required. This means you don’t have to keep
track of memory pointers or manually deallocate memory. This feature
means a program is less likely to crash and that memory can’t be
intentionally misused.
This book is intended to serve programmers who use other languages as a
general introduction to the Java programming language. It introduces the
reader to the major elements of Java programming and provides links and
suggested reading for further exploration. You may order most book titles
from Fatbrain (http://fatbrain.com/). Unlinked titles may be out of print,
but are still useful and generally available. For more extensive lists of
resources, see “Learning more about Java” in the Quick Start .
This book includes the following chapters:
• Java syntax: Chapter 2, “Java language elements”, Chapter 3, “Java
language structure”, and Chapter 4, “Java language control.”
These three chapters define basic Java syntax and introduce you to basic
object-oriented programming concepts. They also suggest other resources.
These suggestions are simply starting points. Anyone learning Java from
books may need three or four books to learn from, since different books
have different emphases, and none covers the language completely.
Introduction 1-1
Chapter
22942416.001.png
Zgłoś jeśli naruszono regulamin