Download Introduction to Java 2 Programming

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Introduction to Java 2
Programming
Lecture 10
API Review; Where Next
Overview
• Reviewing the Java API
– Some packages we’ve seen
– And some new ones…
• Coding and Design Tips
– Tips for good Java programs
• Where Next?
– Books, Websites, Tutorials
java.lang
• The real core of the Java API
– Automatically imported by all Java classes
• System and Runtime classes
– Access to the virtual machine
– System properties
– Standard input, output, error
• The Number classes
– Boolean, Integer, Float, Double, Long, Short, etc
– Provide OO version of primitive types
– Provide conversion functions (e.g. to/from String)
• The Class and Object classes
– Object is the root of the Java inheritance tree
– Class is an OO description of a Java class
java.util
• Large collection utility classes, and several subpackages
• Collection classes, very flexible data structures
– Implementations of Set, List and Map interfaces
– Arrays and Collections classes, for sorting and
searching collections
• Calendar and Date classes for providing date
utilities
• Other miscellaneous classes
– Currency, StringTokenizer, etc
java.util
• java.util.logging
– Provides access to a complete logging system, for recording
application progress, error messages, etc
– Very flexible (different log levels, different log formats, different
log destinations)
• java.util.zip, java.util.jar
– Classes for working with zip and jar files.
– Built in compression libraries
• java.util.regex
– “Regular Expressions”
– Pattern matching system adopted from Perl
– Very flexible way to process text strings (e.g. to extract substrings,
find data, etc)
java.io
• The Java I/O classes
• InputStreams and OutputStreams
– Reading and writing data
• Readers and Writers
– Reading and writing text (according to a character set)
• File
– OO representation of the file system (both files and
directories)
java.net
• Socket
– Standard Berkeley socket implementation for
connecting to remote service
– ServerSocket class for writing servers
• URL
– Describes a remote website, and gives access to its data
via a Stream
• HTTPURLConnection
– Provides more detailed access to a remote website
– E.g. follow redirects, write to remote website, etc
– (but not as flexible as a true HTTP library)
java.rmi
• “Remote Method Invocation”
• A standard way to call methods on objects on a
remote machine
– No need to deal with networking!
• A Registry lists all the useful objects on the remote
machine
– Client gets reference to object from a registry, and then
uses it as normal
• Remote objects have to implement an special
interface
– All methods throw RemoteException
• Very powerful
java.sql
• Java database access layer
– Known as JDBC (“Java DataBase Connectivity”)
• Allows a Java application to
– ..get a Connection to a database
– …create and execute Statements to query the database
– …and then process the ResultSet that is returned
• All major database vendors have implemented a
Java “JDBC Driver”
– The core API describes the basic interfaces
– The vendor implements that for their database system
java.awt
• “Abstract Windowing Toolkit”
– The original way to build GUI applications in Java
• Limited capabilities
– Few components
– API poorly designed (particularly event handling)
– Not entirely portable (not pure Java)
• Still used for some applications, but has been
replaced…
javax.swing
• Java Foundation Classes (“Swing”)
– Replacement for AWT (but does share some common
classes)
• Much more flexible
–
–
–
–
–
100% Java (therefore truly portable)
More components, more sophisticated
Pluggable “Look and Feel” support
Better graphics support
Support for Drag and Drop, etc
• Some downsides, though
– Can be slower than AWT
– Memory hungry
– Large and complex API (to meet all requirements)
Overview
• Reviewing the Java API
– Some packages we’ve seen
– And some new ones…
• Coding and Design Tips
– Tips for good Java programs
• Where Next?
– Books, Websites, Tutorials
Java Coding Tips
• Never re-invent the wheel!
• Never write a new class, if an existing one
can be extended in some way
– Either through inheritance (or wrapping)
• Look for third-party APIs
– E.g. jakarta.apache.org
• Only then write something yourself
Java Coding Tips
• Make good use of polymorphism
– Use base class references where possible
• Allows you to change the concrete implementation
• Make good use of inheritance
– Add functionality by extending. Don’t rewrite original
class
– But don’t overuse it, aggregation is often better
• Never break encapsulation
– Practice “defensive programming”
– Don’t return objects you don’t want changed, copy
them instead.
Java Coding Tips
• Use Collections and Iterators, not arrays and for
loops
– More functionality, easier to work with
• Spend time thinking about your object model
– …but don’t worry about getting it right first time
• Learn from others
– Read other peoples code, see how they’ve tackled
similar problems
Java Coding Tips
• Best tip of all:
• Get to know the core Java API
– Read through the Javadoc
– Try building simple test classes to manipulate
the objects
Where Next?
• The Java Tutorial
– Covers all the core functionality very thoroughly
– Divided into “trails” so easy to dip into to learn about
specific functionality
– (Some bits aren’t as up to date as they might be,
however)
• “Thinking in Java” (Bruce Eckel)
– Excellent OO introduction to Java programming
• “Design Patterns” (Gang of Four)
– Recipes for designing OO applications
• “Refactoring” (Martin Fowler)
– Improving the design of existing OO code