You are previewing Java™: A Beginner's Tutorial, Second Edition.
O'Reilly logo
Java™: A Beginner's Tutorial, Second Edition

Book Description

A Java tutorial for beginners, covering Java SE 6

Table of Contents

  1. Copyright
  2. Introduction
    1. Java, the Language and the Technology
      1. What Makes Java Platform Independent?
      2. JDK, JRE, JVM, What's the Difference?
      3. Java 2, J2SE, J2EE, J2ME, Java 6, What Are They?
      4. The Java Community Process (JCP) Program
    2. An Overview of Object-Oriented Programming
      1. The Benefits of OOP
      2. Is OOP Hard?
    3. About This Book
    4. Downloading and Installing Java
      1. Downloading and Installing the JDK
        1. Installation on Windows
        2. Installation on UNIX and Linux
      2. Configuring System Environment Variables
        1. Setting the Path Environment Variable on Windows
        2. Setting the Path Environment Variable on UNIX and Linux
      3. Testing the Installation
      4. Installing Java API Documentation
    5. Downloading Program Examples and Answers
  3. 1. Your First Taste of Java
    1. Your First Java Program
      1. Writing a Java Program
      2. Compiling Your Java Program
      3. Running Your Java Program
    2. Java Code Conventions
    3. Integrated Development Environments (IDEs)
    4. Summary
    5. Questions
  4. 2. Language Fundamentals
    1. ASCII and Unicode
    2. Separators
    3. Primitives
    4. Variables
      1. Java Letters and Java Digits
      2. Sun's Naming Conventions for Variables
    5. Constants
    6. Literals
      1. Integer Literals
      2. Floating-Point Literals
      3. Boolean Literals
      4. Character Literals
    7. Primitive Conversions
      1. The Widening Conversion
      2. The Narrowing Conversion
    8. Operators
      1. Unary Operators
        1. Unary Minus Operator –
        2. Unary Plus Operator +
        3. Increment Operator ++
        4. Decrement Operator --
        5. Logical Complement Operator!
        6. Bitwise Complement Operator ~
      2. Arithmetic Operators
        1. Addition Operator +
        2. Subtraction Operator –
        3. Multiplication Operator *
        4. Division Operator /
        5. Modulus Operator %
      3. Equality Operators
      4. Relational Operators
      5. Conditional Operators
        1. The && operator
        2. The || Operator
        3. The ? : Operator
      6. Shift Operators
        1. The Left Shift Operator <<
        2. The Right Shift Operator >>
        3. The Unsigned Right Shift Operator >>>
      7. Assignment Operators
      8. Integer Bitwise Operators & | ^
      9. Logical Operators & | ^
      10. Operator Precedence
      11. Promotion
    9. Comments
    10. Summary
    11. Questions
  5. 3. Statements
    1. An Overview of Java Statements
    2. The if Statement
    3. The while Statement
    4. The do-while Statement
    5. The for Statement
    6. The break Statement
    7. The continue Statement
    8. The switch Statement
    9. Summary
    10. Questions
  6. 4. Objects and Classes
    1. What Is a Java Object?
    2. Java Classes
      1. Fields
      2. Methods
      3. The Method main
      4. Constructors
      5. Class Members in UML Class Diagrams
    3. Creating Objects
    4. The null Keyword
    5. Objects in Memory
    6. Java Packages
    7. Encapsulation and Access Control
      1. Class Access Control Modifiers
      2. Class Member Access Control Modifiers
    8. The this Keyword
    9. Using Other Classes
    10. Final Variables
    11. Static Members
    12. Static Final Variables
    13. Static import
    14. Variable Scope
    15. Method Overloading
    16. By Value or By Reference?
    17. Loading, Linking, and Initialization
      1. Loading
      2. Linking
      3. Initialization
    18. Object Creation Initialization
    19. Comparing Objects
    20. The Garbage Collector
    21. Summary
    22. Questions
  7. 5. Core Classes
    1. java.lang.Object
    2. java.lang.String
      1. Comparing Two Strings
      2. String Literals
      3. Escaping Certain Characters
      4. The String Class's Constructors
      5. The String Class's Methods
    3. java.lang.StringBuffer and java.lang.StringBuilde
      1. StringBuilder Class's Constructors
      2. StringBuilder Class's Methods
    4. Primitive Wrappers
      1. java.lang.Integer
      2. java.lang.Boolean
      3. java.lang.Character
    5. Arrays
      1. Iterating over an Array
      2. Changing an Array Size
      3. Passing a String Array to main
    6. java.lang.Class
    7. java.lang.System
    8. java.util.Scanner
    9. Boxing and Unboxing
    10. Varargs
    11. The format and printf Methods
    12. Summary
    13. Questions
  8. 6. Inheritance
    1. An Overview of Inheritance
      1. The extends Keyword
      2. The is-a Relationship
    2. Accessibility
    3. Method Overriding
    4. Calling the Superclass's Constructors
    5. Calling the Superclass's Hidden Members
    6. Type Casting
    7. Final Classes
    8. The instanceof Keyword
    9. Summary
    10. Questions
  9. 7. Error Handling
    1. Catching Exceptions
    2. The java.lang.Exception Class
    3. Throwing an Exception from a Method
    4. User-Defined Exceptions
    5. Final Words on Exception Handling
    6. Summary
    7. Question
  10. 8. Numbers and Dates
    1. Number Parsing
    2. Number Formatting
    3. Number Parsing with java.text.NumberFormat
    4. The java.lang.Math Class
    5. The java.util.Date Class
    6. The java.util.Calendar Class
    7. Date Parsing and Formatting with DateFormat
      1. DateFormat
      2. SimpleDateFormat
    8. Summary
    9. Questions
  11. 9. Interfaces and Abstract Classes
    1. The Concept of Interface
    2. The Interface, Technically Speaking
      1. Fields in an Interface
      2. Methods
    3. Base Classes
    4. Abstract Classes
    5. Summary
    6. Questions
  12. 10. Enums
    1. An Overview of Enum
    2. Enums in a Class
    3. The java.lang.Enum Class
    4. Iterating Enumerated Values
    5. Switching on Enum
    6. Summary
    7. Questions
  13. 11. The Collections Framework
      1. Note on Generics
    1. An Overview of the Collections Framework
    2. The Collection Interface
    3. List and ArrayList
    4. Iterating Over a Collection with Iterator and for
    5. Set and HashSet
    6. Queue and LinkedList
    7. Collection Conversion
    8. Map and HashMap
    9. Making Objects Comparable and Sortable
      1. Using java.lang.Comparable
      2. Using Comparable and Comparator
    10. Summary
    11. Questions
  14. 12. Generics
    1. Life without Generics
    2. Introducing Generic Types
    3. Using Generic Types without Type Parameters
      1. Warning
    4. Using the ? Wildcard
    5. Using Bounded Wildcards in Methods
      1. Lower Bounds
    6. Writing Generic Types
    7. Summary
    8. Questions
  15. 13. Input Output
    1. The File Class
      1. File Constructors
      2. File Methods
    2. The Concept of Input/Output Streams
    3. Reading Binary Data
      1. InputStream
      2. FileInputStream
      3. BufferedInputStream
    4. Writing Binary Data
      1. OutputStream
      2. FileOutputStream
      3. BufferedOutputStream
    5. Writing Text (Characters)
      1. Writer
      2. OutputStreamWriter
      3. FileWriter
      4. PrintWriter
      5. BufferedWriter
    6. Reading Text (Characters)
      1. Reader
      2. InputStreamReader
      3. FileReader
      4. BufferedReader
    7. Logging with PrintStream
    8. RandomAccessFile
    9. Object Serialization
    10. Summary
    11. Questions
  16. 14. Nested and Inner Classes
    1. An Overview of Nested Classes
    2. Static Nested Classes
    3. Member Inner Classes
    4. Local Inner Classes
    5. Anonymous Inner Classes
    6. Behind Nested and Inner Classes
    7. Summary
    8. Questions
  17. 15. Swing Basics
    1. AWT Components
    2. Useful AWT Classes
      1. java.awt.Color
      2. java.awt.Font
      3. java.awt.Point
      4. java.awt.Dimension
      5. java.awt.Rectangle
      6. java.awt.Graphics
      7. java.awt.Toolkit
    3. Basic Swing Components
      1. JFrame
      2. Resizing and Positioning
      3. Extending JFrame
      4. JComponent
      5. Icon and ImageIcon
      6. JLabel
      7. JButton
      8. JTextField and JPasswordField
      9. JTextArea
      10. JCheckbox
      11. JRadioButton
      12. JList
      13. JComboBox
      14. JDialog
      15. JOptionPane
      16. Using JOptionPane to Display a Message
      17. Using JOptionPane to Prompt User Confirmation
      18. Using JOptionPane to Obtain User Input
      19. JFileChooser
    4. Summary
    5. Questions
  18. 16. Swinging Higher
    1. Layout Managers
      1. BorderLayout
      2. FlowLayout
      3. BoxLayout
      4. GridLayout
      5. No LayoutManagers
    2. Event Handling
      1. The Java Event Model
        1. The Event Source
        2. The Event Object
        3. Event Listeners
      2. Swing Event Handling
      3. The AWT Event API
        1. The java.awt.event.ActionEvent Class
        2. The java.awt.event.ActionListener Interface
        3. The java.awt.event.MouseEvent Class
        4. The java.awt.event.MouseListener Interface
        5. The java.awt.event.KeyEvent Class
        6. The java.awt.event.KeyListener Interface
        7. The java.awt.event.WindowEvent Class
        8. The java.awt.event.WindowListener Interface
      4. Handling ActionEvent
      5. Handling MouseEvent
      6. Writing a Listener as an Anonymous Class
      7. Handling ActionEvent of JRadioButton
      8. Handling KeyEvent
      9. Handling WindowListener
    3. Working with Menus
    4. The Look and Feel
      1. Choosing the Look and Feel
    5. Fast Splash Screens
    6. System Tray Support
    7. Desktop Help Applications
    8. Summary
    9. Questions
  19. 17. Polymorphism
    1. Defining Polymorphism
    2. Polymorphism in Action
    3. Polymorphism in a Drawing Application
    4. Polymorphism and Reflection
    5. Summary
    6. Questions
  20. 18. Annotations
    1. An Overview of Annotations
      1. Annotations and Annotation Types
      2. Annotation Syntax
      3. The Annotation Interface
    2. Standard Annotations
      1. Override
      2. Deprecated
      3. SuppressWarnings
    3. Common Annotations
    4. Standard Meta-Annotations
      1. Documented
      2. Inherited
      3. Retention
      4. Target
    5. Custom Annotation Types
      1. A Custom Annotation Type
      2. Using Custom Annotation Types
      3. Using Reflection to Query Annotations
    6. Summary
    7. Questions
  21. 19. Internationalization
    1. Locales
    2. Internationalizing Applications
      1. Isolating Textual Components into Properties Files
      2. Reading Properties Files using ResourceBundle
    3. An Internationalized Swing Application
    4. Summary
    5. Questions
  22. 20. Applets
    1. A Brief History of Applets
    2. The Applet API
      1. The Applet Class
      2. The AppletContext Interface
      3. The AudioClip Interface
      4. The AppletStub Interface
    3. Security Restrictions
    4. Writing and Deploying Applets
    5. How AppletViewer Works
    6. Passing Parameters to an Applet
    7. SoundPlayerApplet
    8. JApplet
    9. Applet Deployment in a JAR File
    10. Faster Loading
    11. Summary
    12. Questions
  23. 21. Java Networking
    1. An Overview of Networking
    2. The Hypertext Transfer Protocol (HTTP)
      1. HTTP Requests
      2. HTTP Responses
    3. java.net.URL
      1. Parsing a URL
      2. Reading A Web Resource
    4. java.net.URLConnection
      1. Reading Web Resources
      2. Writing to a Web server
    5. java.net.Socket
    6. java.net.ServerSocket
    7. A Web Server Application
      1. The HttpServer Class
      2. The Request Class
      3. The Response Class
      4. Running the Application
    8. Summary
    9. Questions
  24. 22. Java Database Connectivity
    1. Introduction to JDBC
    2. Four Steps to Data Access
      1. Loading the JDBC Driver
      2. Obtaining a Database Connection
      3. Creating A Statement Object
      4. Creating a ResultSet Object
    3. Closing JDBC Objects
    4. Reading Metadata
    5. The SQLTool Example
      1. Preparing MySQL
      2. Downloading the Software
      3. Installing and Running MySQL
      4. Creating the Customers table
      5. The SQLTool Class
    6. Summary
    7. Questions
  25. 23. Java Threads
    1. Introduction to Java Threads
    2. Creating a Thread
      1. Extending Thread
        1. Implementing Runnable
    3. Working with Multiple Threads
    4. Thread Priority
    5. Stopping a Thread
    6. Synchronization
      1. Thread Interference
      2. Atomic Operations
        1. Thread Safety
      3. Method Synchronization
      4. Mutual Exclusion and Visibility
      5. Block Synchronization
    7. Visibility
    8. Thread Coordination
    9. Using Timers
    10. Swing Timers
    11. Summary
    12. Questions
  26. 24. Concurrency Utilities
    1. Atomic Variables
    2. Executor and ExecutorService
    3. Callable and Future
    4. Swing Worker
    5. Locks
    6. Questions
  27. 25. Security
    1. A Java Security Overview
    2. Using the Security Manager
    3. Policy Files
      1. keystore
      2. grant
    4. Permissions
      1. java.io.FilePermission
      2. java.security.BasicPermission
      3. java.util.PropertyPermission
      4. java.net.SocketPermission
      5. java.security.UnresolvedPermission
      6. java.lang.RuntimePermission
      7. java.awt.AWTPermission
      8. java.net.NetPermission
      9. java.lang.reflect.ReflectPermission
      10. java.io.SerializablePermission
      11. java.security.SecurityPermission
      12. java.security.AllPermission
      13. javax.security.auth.AuthPermission
    5. Using the Policy Tool
    6. Applet Security
    7. Programming with Security
    8. Cryptography Overview
      1. Encryption/Decryption
      2. Authentication
      3. Data Integrity
      4. How SSL Works
    9. Creating Certificates
    10. The KeyTool Program
      1. Generating Key Pairs
      2. Getting Certified
      3. Importing a Certificate into the Keystore
      4. Exporting a Certificate from the Keystore
      5. Listing Keystore Entries
    11. The JarSigner Tool
      1. Signing JAR Files
      2. Verifying Signed JAR Files
      3. An Example: Signing an Applet
    12. Java Cryptography API
    13. Summary
    14. Questions
  28. 26. Java Web Applications
    1. The Architecture of a Servlet Application
      1. Servlet Containers and Java EE Containers
    2. Servlet API Overview
      1. The javax.servlet Package
    3. Servlet
    4. Writing a Basic Servlet Application
      1. Installing Tomcat
      2. Writing and Compiling the Servlet Class
      3. Deployment Descriptor
      4. Preparing the Application Directory
      5. Deploying the Application to Tomcat
      6. Invoking the Servlet
    5. ServletResponse
    6. ServletRequest
    7. ServletConfig
    8. GenericServlet
    9. ServletContext
    10. HTTP Servlets
      1. HttpServlet
      2. HttpServletRequest
      3. HttpServletResponse
      4. Writing an Http Servlet
    11. Summary
    12. Questions
  29. 27. JavaServer Pages
    1. A JSP Overview
    2. jspInit, jspDestroy, and Other Methods
    3. Implicit Objects
    4. JSP Syntactic Elements
      1. Directives
        1. The page Directive
        2. The include Directive
      2. Scripting Elements
        1. Scriptlets
        2. Expressions
        3. Declarations
    5. Handling Errors
    6. Summary
    7. Questions
  30. 28. Javadoc
    1. Writing Documentation in Java Classes
      1. @author
      2. {@code}
      3. {@docRoot}
      4. @deprecated
      5. @exception
      6. {@inheritDoc}
      7. {@link}
      8. {@linkplain}
      9. {@literal}
      10. @param
      11. @return
      12. @see
      13. @serial
      14. @serialData
      15. @serialField
      16. @since
      17. @throws
      18. {@value}
      19. @version
    2. Javadoc Syntax
      1. Javadoc Options
      2. Standard Doclet Options
      3. Generated Documents
    3. Summary
    4. Question
  31. 29. Application Deployment
    1. JWS Overview
    2. JNLP File Syntax
      1. The jnlp Element
      2. The information Element
      3. The security Element
      4. The resources Element
      5. The application-desc Element
      6. The applet-desc Element
    3. A Deployment Example
      1. Configure the Web server to use the Java Web Start MIME type
      2. Create a jnlp file
      3. 3. Create an HTML File
      4. 4. Test the deployment
    4. Security Concerns
    5. Summary
    6. Questions
  32. A. javac
    1. Options
      1. Standard Options
      2. Nonstandard Options
      3. The -J Option
    2. Command Line Argument Files
  33. B. java
    1. Options
      1. Standard Options
      2. Nonstandard Options
  34. C. jar
    1. Syntax
    2. Options
    3. Examples
      1. Create
      2. Update
      3. List
      4. Extract
      5. Index
    4. Setting an Application's Entry Point
  35. D. NetBeans
    1. Download and Installation
    2. Creating a Project
    3. Creating a Class
    4. Running a Java Class
    5. Adding Libraries
    6. Debugging Code
  36. E. Eclipse
    1. Download and Installation
    2. Creating a Project
    3. Creating a Class
    4. Running a Java Class
    5. Adding Libraries
    6. Debugging Code