You are previewing Java All-in-One For Dummies, 4th Edition.
O'Reilly logo
Java All-in-One For Dummies, 4th Edition

Book Description

Everything you need to get going with Java!

Java All-in-One For Dummies, 4th Edition has what you need to get up and running quickly with Java. Covering the enhanced mobile development and syntax features as well as programming improvements, this guide makes it easy to find what you want and put it to use.

  • Focuses on the vital information that enables you to get up and running quickly with Java

  • Covers the enhanced multimedia features as well as programming enhancements, Java and XML, Swing, server-side Java, Eclipse, and more

  • Minibooks cover Java basics; programming basics; strings, arrays, and collections; programming techniques; Swing; Web programming; files and databases; and a "fun and games" category

  • Java All-in-One For Dummies, 4th Edition focuses on the practical information you need to become productive with Java right away.

    Table of Contents

      1. Introduction
        1. About this Book
        2. Foolish Assumptions
        3. Icons Used in This Book
        4. Beyond the Book
        5. Where to Go from Here
      2. Book I: Getting Started with Java Basics
        1. Chapter 1: Welcome to Java
          1. What Is Java, and Why Is It So Great?
            1. Platform independence
            2. Object orientation
            3. The Java API
            4. The Internet
          2. Java versus Other Languages
          3. Important Features of the Java Language
            1. Type checking
            2. Automatic memory management
            3. Exception handling
          4. On the Downside: Java's Weaknesses
          5. Java Version Insanity
          6. What's in a Name?
        2. Chapter 2: Installing and Using Java Tools
          1. Downloading and Installing the Java Development Kit
            1. Downloading the JDK
            2. Installing the JDK
            3. Perusing the JDK folders
            4. Setting the path
          2. Using Java's Command-Line Tools
            1. Compiling a program
            2. Compiling more than one file
            3. Using Java compiler options
            4. Running a Java program
            5. Using the javap command
          3. Using Java Documentation
            1. Java SE API Docs
            2. Java Language Specification
        3. Chapter 3: Working with TextPad
          1. Downloading and Installing TextPad
          2. Editing Source Files
          3. Compiling a Program
          4. Running a Java Program
          5. Running an Applet
      3. Book II: Programming Basics
        1. Chapter 1: Java Programming Basics
          1. Looking at the Infamous Hello, World! Program
          2. Dealing with Keywords
          3. Working with Statements
            1. Types of statements
            2. White space
          4. Working with Blocks
          5. Creating Identifiers
          6. Crafting Comments
            1. End-of-line comments
            2. Traditional comments
            3. JavaDoc comments
          7. Introducing Object-Oriented Programming
            1. Understanding classes and objects
            2. Understanding static methods
            3. Creating an object from a class
            4. Viewing a program that uses an object
            5. So what's the difference?
          8. Importing Java API Classes
        2. Chapter 2: Working with Variables and Data Types
          1. Declaring Variables
            1. Declaring two or more variables in one statement
            2. Declaring class variables
            3. Declaring instance variables
            4. Declaring local variables
          2. Initializing Variables
            1. Initializing variables with assignment statements
            2. Initializing variables with initializers
          3. Using Final Variables (Constants)
          4. Working with Primitive Data Types
            1. Integer types
            2. Floating-point types
            3. The char type
            4. The Boolean type
            5. Wrapper classes
          5. Using Reference Types
          6. Working with Strings
            1. Declaring and initializing strings
            2. Combining strings
            3. Converting primitives to strings
            4. Converting strings to primitives
          7. Converting and Casting Numeric Data
            1. Automatic conversions
            2. Type casting
          8. Thinking Inside the Box
          9. Understanding Scope
          10. Shadowing Variables
          11. Printing Data with System.out
            1. Using standard input and output streams
            2. Using System.out and System.err
          12. Getting Input with the Scanner Class
            1. Importing the Scanner class
            2. Declaring and creating a Scanner object
            3. Getting input
          13. Getting Input with the JOptionPane Class
          14. Using enum to Create Your Own Data Types
        3. Chapter 3: Working with Numbers and Expressions
          1. Working with Arithmetic Operators
          2. Dividing Integers
          3. Combining Operators
          4. Using the Unary Plus and Minus Operators
          5. Using Increment and Decrement Operators
          6. Using the Assignment Operator
          7. Using Compound Assignment Operators
          8. Using the Math Class
            1. Using constants of the Math class
            2. Working with mathematical functions
            3. Creating random numbers
            4. Rounding functions
          9. Formatting Numbers
          10. Recognizing Weird Things about Java Math
            1. Integer overflow
            2. Floating-point weirdness
            3. Division by zero
        4. Chapter 4: Making Choices
          1. Using Simple Boolean Expressions
          2. Using if Statements
            1. Simple if statements
            2. if-else statements
            3. Nested if statements
            4. else-if statements
          3. Using Mr. Spock's Favorite Operators (Logical Ones, of Course)
            1. Using the ! operator
            2. Using the & and && operators
            3. Using the | and || operators
            4. Using the ^ operator
            5. Combining logical operators
          4. Using the Conditional Operator
          5. Comparing Strings
        5. Chapter 5: Going Around in Circles (Or, Using Loops)
          1. Using Your Basic while Loop
            1. The while statement
            2. A counting loop
          2. Breaking Out of a Loop
          3. Looping Forever
            1. Letting the user decide when to quit
            2. Letting the user decide in another way
          4. Using the continue Statement
          5. Running do-while Loops
          6. Validating Input from the User
          7. Using the Famous for Loop
            1. Understanding the formal format of the for loop
            2. Scoping out the counter variable
            3. Counting even numbers
            4. Counting backward
            5. Using for loops without bodies
            6. Ganging up your expressions
            7. Omitting expressions
            8. Breaking and continuing your for loops
          8. Nesting Your Loops
            1. A simple nested for loop
            2. A guessing game
        6. Chapter 6: Pulling a Switcheroo
          1. Battling else-if Monstrosities
            1. Viewing an example else-if program
            2. Creating a better version of the example program
          2. Using the switch Statement
            1. Viewing a boring switch example, complete with flowchart
            2. Putting if statements inside switch statements
          3. Creating Character Cases
          4. Intentionally Leaving Out a Break Statement
          5. Switching with Strings
        7. Chapter 7: Adding Some Methods to Your Madness
          1. The Joy of Methods
          2. The Basics of Making Methods
            1. An example
            2. Another example
          3. Methods That Return Values
            1. Declaring the method's return type
            2. Using the return statement to return the value
            3. Using a method that returns a type
            4. You gotta have a proper return statement
            5. Trying another version of theguessing-game program
          4. Methods That Take Parameters
            1. Declaring parameters
            2. Scoping out parameters
            3. Understanding pass-by-value
            4. Trying yet another version ofthe guessing-game program
        8. Chapter 8: Handling Exceptions
          1. Understanding Exceptions
            1. Witnessing an exception
            2. Finding the culprit
          2. Catching Exceptions
            1. A simple example
            2. Another example
          3. Handling Exceptions with a Preemptive Strike
          4. Catching All Exceptions at Once
          5. Displaying the Exception Message
          6. Using a finally Block
          7. Handling Checked Exceptions
            1. Viewing the catch-or-throw compiler error
            2. Catching FileNotFoundException
            3. Throwing the FileNotFoundException
            4. Throwing an exception from main
            5. Swallowing exceptions
          8. Throwing Your Own Exceptions
          9. Catching Multiple Exceptions
      4. Book III: Object-Oriented Programming
        1. Chapter 1: Understanding Object-Oriented Programming
          1. What Is Object-Oriented Programming?
          2. Understanding Objects
            1. Objects have identity
            2. Objects have type
            3. Objects have state
            4. Objects have behavior
          3. Understanding the Life Cycle of an Object
          4. Working with Related Classes
            1. Inheritance
            2. Interfaces
          5. Designing a Program with Objects
          6. Diagramming Classes with UML
            1. Drawing classes
            2. Drawing arrows
        2. Chapter 2: Making Your Own Classes
          1. Declaring a Class
            1. Picking class names
            2. Knowing what goes in the class body
            3. Seeing where classes go
          2. Working with Members
            1. Understanding fields
            2. Understanding methods
            3. Understanding visibility
          3. Using Getters and Setters
          4. Overloading Methods
          5. Creating Constructors
            1. Creating basic constructors
            2. Creating default constructors
            3. Calling other constructors
          6. Finding More Uses for the this Keyword
          7. Using Initializers
        3. Chapter 3: Working with Statics
          1. Understanding Static Fields and Methods
          2. Working with Static Fields
          3. Using Static Methods
          4. Counting Instances
          5. Preventing Instances
          6. Using Static Initializers
        4. Chapter 4: Using Subclasses and Inheritance
          1. Introducing Inheritance
            1. Motorcycles, trains, and automobiles
            2. Game play
            3. A businesslike example
            4. Inheritance hierarchies
          2. Creating Subclasses
          3. Overriding Methods
          4. Protecting Your Members
          5. Using this and super in Your Subclasses
          6. Understanding Inheritance and Constructors
          7. Using final
            1. Final methods
            2. Final classes
          8. Casting Up and Down
          9. Determining an Object's Type
          10. Poly What?
          11. Creating Custom Exceptions
            1. Tracing the Throwable hierarchy
            2. Creating an exception class
            3. Throwing a custom exception
        5. Chapter 5: Using Abstract Classes and Interfaces
          1. Using Abstract Classes
          2. Using Interfaces
            1. Creating a basic interface
            2. Implementing an interface
            3. Using an interface as a type
          3. More Things You Can Do with Interfaces
            1. Adding fields to an interface
            2. Extending interfaces
            3. Using interfaces for callbacks
          4. Using Default Methods
        6. Chapter 6: Using the Object and Class Classes
          1. The Mother of All Classes: Object
            1. Every object is an Object
            2. Object as a type
            3. Methods of the Object class
            4. Primitives aren't objects
          2. The toString Method
            1. Using toString
            2. Overriding toString
          3. The equals Method
            1. Using equals
            2. Overriding the equals method
          4. The clone Method
            1. Implementing the clone method
            2. Using clone to create a shallow copy
            3. Creating deep copies
          5. The Class Class
        7. Chapter 7: Using Inner Classes, Anonymous Classes, and Lambda Expressions
          1. Declaring Inner Classes
            1. Understanding inner classes
            2. Viewing an example
          2. Using Static Inner Classes
        8. Chapter 8: Packaging and Documenting Your Classes
          1. Working with Packages
            1. Importing classes and packages
            2. Creating your own packages
            3. An example
          2. Putting Your Classes in a JAR File
            1. jar command-line options
            2. Archiving a package
            3. Adding a jar to your classpath
            4. Running a program directly from an archive
          3. Using JavaDoc to Document Your Classes
          4. Adding JavaDoc comments
          5. Using the javadoc command
          6. Viewing JavaDoc pages
      5. Book IV: Strings, Arrays, and Collections
        1. Chapter 1: Working with Strings
          1. Reviewing Strings
          2. Using the String Class
            1. Finding the length of a string
            2. Making simple string modifications
            3. Extracting characters from a string
            4. Extracting substrings from a string
            5. Splitting a string
            6. Replacing parts of a string
          3. Using the StringBuilder and StringBuffer Classes
            1. Creating a StringBuilder object
            2. Using StringBuilder methods
            3. Viewing a StringBuilder example
          4. Using the CharSequence Interface
        2. Chapter 2: Using Arrays
          1. Understanding Arrays
          2. Creating Arrays
          3. Initializing an Array
          4. Using for Loops with Arrays
          5. Solving Homework Problems with Arrays
          6. Using the Enhanced for Loop
          7. Using Arrays with Methods
          8. Using Two-Dimensional Arrays
            1. Creating a two-dimensional array
            2. Accessing two-dimensional array elements
            3. Initializing a two-dimensional array
            4. Using jagged arrays
            5. Going beyond two dimensions
          9. Working with a Fun but Complicated Example: A Chessboard
          10. Using the Arrays Class
            1. Filling an array
            2. Copying an array
            3. Sorting an array
            4. Searching an array
            5. Comparing arrays
            6. Converting arrays to strings
        3. Chapter 3: Using the ArrayList Class
          1. Understanding the ArrayList Class
          2. Creating an ArrayList Object
          3. Adding Elements
          4. Accessing Elements
          5. Printing an ArrayList
          6. Using an Iterator
          7. Updating Elements
          8. Deleting Elements
        4. Chapter 4: Using the LinkedList Class
          1. Understanding the LinkedList Class
          2. Creating a LinkedList
          3. Adding Items to a LinkedList
          4. Retrieving Items from a LinkedList
          5. Updating LinkedList Items
          6. Removing LinkedList Items
        5. Chapter 5: Creating Generic Collection Classes
          1. Why Generics?
          2. Creating a Generic Class
          3. A Generic Stack Class
          4. Using Wildcard-Type Parameters
          5. A Generic Queue Class
          6. Using the Diamond Operator
        6. Chapter 6: Using Bulk Data Operations with Collections
          1. Looking At a Basic Bulk Data Operation
          2. Looking Closer at the Stream Interface
          3. Using Parallel Streams
      6. Book V: Programming Techniques
        1. Chapter 1: Programming Threads
          1. Understanding Threads
          2. Creating a Thread
            1. Understanding the Thread class
            2. Extending the Thread class
            3. Creating and starting a thread
          3. Implementing the Runnable Interface
            1. Using the Runnable interface
            2. Creating a class that implements Runnable
            3. Using the CountDownApp class
          4. Creating Threads That Work Together
          5. Using an Executor
          6. Synchronizing Methods
          7. Creating a Lock
          8. Coping with Threadus Interruptus
            1. Finding out whether you've been interrupted
            2. Aborting the countdown
        2. Chapter 2: Network Programming
          1. Understanding Network Programming
            1. IP addresses and ports
            2. Host names, DNS, and URLs
            3. telnet
          2. Getting Information about Internet Hosts
            1. The InetAddress class
            2. A program that looks up host names
          3. Creating Network Server Applications
            1. The Socket class
            2. The ServerSocket class
          4. Introducing BART
            1. The BartQuote class
            2. The BartServer program
            3. The BartClient program
          5. Working with BartServer 2.0
        3. Chapter 3: Using Regular Expressions
          1. Creating a Program for Experimentingwith Regular Expressions
          2. Performing Basic Character Matching
            1. Matching single characters
            2. Using predefined character classes
            3. Using custom character classes
            4. Using ranges
            5. Using negation
            6. Matching multiple characters
            7. Using escapes
            8. Using parentheses to group characters
            9. Using the pipe symbol
          3. Using Regular Expressions in Java Programs
            1. Understanding the String problem
            2. Using regular expressions with the String class
            3. Using the Pattern and Matcher classes
        4. Chapter 4: Using Recursion
          1. Calculating the Classic Factorial Example
            1. The nonrecursive solution
            2. The recursive solution
          2. Displaying Directories
          3. Writing Your Own Sorting Routine
            1. Understanding how Quicksort works
            2. Using the sort method
            3. Using the partition method
            4. Putting it all together
        5. Chapter 5: Working with Dates and Times
          1. Pondering How Time is Represented
          2. Picking the Right Date and Time Class for Your Application
          3. Using the now Method to Create a Date-Time Object
          4. Using the parse Method to Create a Date-Time Object
          5. Using the of Method to Create a Date-Time Object
            1. Using the Month enumeration
            2. Using the ZoneId class
            3. Using the ZoneOffset class
          6. Looking Closer at the LocalDate Class
          7. Extracting Information About a Date
          8. Comparing Dates
          9. Calculating with Dates
          10. Formatting Dates
          11. Looking at a Fun Birthday Calculator
        6. Chapter 6: Using BigDecimals
          1. Seeing Why Java Can't Add
            1. Double trouble
            2. Another example
          2. BigDecimal to the Rescue!
          3. Creating BigDecimal Objects
          4. Doing BigDecimal Arithmetic
          5. Rounding BigDecimal Values
            1. The fundamentals of scale
            2. The how-to of rounding
          6. Comparing BigDecimal Values
          7. Converting BigDecimals to Strings
          8. Revisiting Sales Tax
      7. Book VI: Swing
        1. Chapter 1: Swinging into Swing
          1. Understanding Some Important Swing Concepts
            1. Seeing what Swing does
            2. Viewing the Swing class hierarchy
          2. I've Been Framed!
            1. Saying Hello, World! in Swing
            2. Positioning the frame onscreen
          3. Using the JPanel Class
          4. Using Labels
          5. Creating Buttons
          6. Controlling the Layout of Components
        2. Chapter 2: Handling Events
          1. Examining Events
          2. Handling Events
          3. Creating a ClickMe Program
          4. Using Inner Classes to Listen for Events
          5. Using Lambda Expressions to Handle Events
          6. Adding an Exit Button
          7. Catching the WindowClosing Event
          8. Revisiting the ClickMe Program
        3. Chapter 3: Getting Input from the User
          1. Using Text Fields
            1. Looking at a sample program
            2. Using text fields for numeric entry
            3. Creating a validation class
          2. Using Text Areas
            1. The JTextArea class
            2. The JScrollPane class
          3. Using Check Boxes
          4. Using Radio Buttons
          5. Using Borders
          6. Using Sliders
          7. Designing a Pizza-Ordering Program
        4. Chapter 4: Choosing from a List
          1. Using Combo Boxes
            1. Creating combo boxes
            2. Getting items from a combo box
            3. Handling combo box events
          2. Using Lists
            1. Creating a list
            2. Getting items from a list
            3. Changing list items
          3. Using Spinners
          4. Using Trees
            1. Building a tree
            2. Creating a JTree component
            3. Getting the selected node
            4. Looking at a complete program that uses a tree
        5. Chapter 5: Using Layout Managers
          1. Working with Layout Managers
            1. Introducing six Swing layout managers
            2. Applying layout managers
          2. Using Flow Layout
          3. Using Border Layout
          4. Using Box Layout
          5. Using Grid Layout
          6. Using GridBag Layout
            1. Sketching out a plan
            2. Adding components to a GridBag layout
            3. Working with GridBagConstraint constructors
            4. Viewing a GridBag layout example
          7. Using Group Layout
      8. Book VII: Web Programming
        1. Chapter 1: Creating Applets
          1. Understanding Applets
          2. Working with the JApplet Class
          3. Looking at a Sample Applet
          4. Creating an HTML Page for an Applet
          5. Testing an Applet
        2. Chapter 2: Creating Servlets
          1. Understanding Servlets
          2. Using Tomcat
            1. Installing and configuring Tomcat
            2. Starting and stopping Tomcat
            3. Testing Tomcat
          3. Creating a Simple Servlet
            1. Importing the servlet packages
            2. Extending the HttpServlet class
            3. Printing to a web page
            4. Responding with HTML
          4. Running a Servlet
          5. Improving the HelloWorld Servlet
          6. Getting Input from the User
            1. Working with forms
            2. Using the InputServlet servlet
          7. Using Classes in a Servlet
        3. Chapter 3: Using JavaServer Pages
          1. Understanding JavaServer Pages
          2. Using Page Directives
          3. Using Expressions
          4. Using Scriptlets
          5. Using Declarations
          6. Using Classes
      9. Book VIII: Files and Databases
        1. Chapter 1: Working with Files
          1. Using the File Class
            1. Knowing the class constructors and methods
            2. Creating a File object
            3. Creating a file
            4. Getting information about a file
            5. Getting the contents of a directory
            6. Renaming files
            7. Deleting a file
          2. Using Command-Line Parameters
          3. Choosing Files in a Swing Application
            1. Creating an Open dialog box
            2. Getting the selected file
            3. Using file filters
          4. Using Path Objects
            1. Creating files with the Paths class
            2. Getting the contents of a directory
          5. Using a File Visitor to Walk a File Tree
        2. Chapter 2: Working with File Streams
          1. Understanding Streams
          2. Reading Character Streams
            1. Creating a BufferedReader
            2. Reading from a character stream
            3. Reading the movies.txt file
          3. Writing Character Streams
            1. Connecting a PrintWriter to a text file
            2. Writing to a character stream
            3. Writing the movies.txt file
          4. Reading Binary Streams
            1. Creating a DataInputStream
            2. Reading from a data input stream
            3. Reading the movies.dat file
          5. Writing Binary Streams
            1. Creating a DataOutputStream
            2. Writing to a binary stream
            3. Writing the movies.dat file
        3. Chapter 3: Database for $100, Please
          1. Defining a Relational Database
          2. Understanding (and Pronouncing) SQL
          3. Introducing SQL Statements
          4. Creating a SQL Database
          5. Querying a Database
            1. Using a basic select statement
            2. Narrowing the query
            3. Excluding rows
            4. Using singleton selects
            5. Finding what something sounds like
            6. Using column functions
            7. Selecting data from more than one table
            8. Eliminating duplicates
          6. Updating and Deleting Rows
            1. Using the delete statement
            2. Using the update statement
        4. Chapter 4: Using JDBC to Connect to a Database
          1. Setting Up a Driver
          2. Connecting to a Database
          3. Querying a Database
            1. Executing a select statement
            2. Navigating through the result set
            3. Getting data from a result set
            4. Putting it all together: A program that reads from a database
          4. Updating SQL Data
          5. Using an Updatable RowSet Object
            1. Deleting a row
            2. Updating the value of a row column
            3. Inserting a row
        5. Chapter 5: Working with XML
          1. Defining XML
            1. Tags
            2. Attributes
            3. The XML declaration
            4. The movies.xml file
          2. Using a DTD
          3. Processing XML in Two Ways: DOM and SAX
          4. Reading a DOM Document
            1. Creating a DocumentBuilderFactory
            2. Configuring the DocumentBuilderFactory
            3. Creating a document builder and the document
            4. Using the getDocument method
          5. Reading DOM Nodes
            1. Processing elements
            2. Getting attribute values
            3. Getting child element values
          6. Putting It All Together: A Program That Lists Movies
      10. Book IX: Fun and Games
        1. Chapter 1: Fun with Fonts and Colors
          1. Working with Fonts
            1. Using font names
            2. Using font styles
            3. Setting a component's font
            4. Getting a list of available fonts
            5. Viewing a program that plays with fonts
          2. Working with Color
            1. Creating colors
            2. Using system colors
            3. Setting the colors of Swing components
            4. Using a color chooser
        2. Chapter 2: Drawing Shapes
          1. Getting a Graphics Context
          2. Drawing Shapes
            1. Tweaking a drawn shape
            2. Seeing how a graphics program creates shapes
          3. Creating Shapes
            1. Creating lines
            2. Creating rectangles
            3. Creating ellipses
            4. Creating arcs
            5. Looking at the ShapeMaker program
          4. Filling Shapes
            1. Drawing transparently
            2. Using a gradient fill
          5. Rotating and Translating
            1. The translate method
            2. The rotate method
          6. Drawing Text
          7. Letting the User Draw on a Component
        3. Chapter 3: Using Images and Sound
          1. Using Images
          2. Using the ImageIcon Class
            1. Using ImageIcon in a Swing application
            2. Using ImageIcon in an applet
          3. Using the Image Class
            1. Creating an Image object
            2. Drawing an Image object
            3. Viewing an Image example
          4. Playing Sounds and Making Music
        4. Chapter 4: Animation and Game Programming
          1. Working with Sprites
            1. Animating a sprite
            2. Creating an animation applet
          2. Understanding Double Buffering
          3. Bouncing the Ball
          4. Bouncing a Bunch of Balls
            1. Creating a Ball class
            2. Animating random balls
          5. Creating Collidable Balls
          6. Playing Games
      11. About the Author
      12. Cheat Sheet
      13. More Dummies Products