1 / 17

Introduction to Java 2 Programming

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

agostino
Download Presentation

Introduction to Java 2 Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Java 2 Programming Lecture 10 API Review; Where Next

  2. 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

  3. 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

  4. java.util • Large collection utility classes, and several sub-packages • 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

  5. 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)

  6. 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)

  7. 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)

  8. 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

  9. 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

  10. 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…

  11. 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)

  12. 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

  13. 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

  14. 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.

  15. 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

  16. 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

  17. 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

More Related