Java
  Home arrow Java arrow Page 3 - Java Comes of Age
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Java Comes of Age
By: Simon White
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 20
    2004-04-07

    Table of Contents:
  • Java Comes of Age
  • Type-Safe Enumerations
  • Static Import and Generics
  • Enhanced For Loop
  • Auto-boxing, Auto-Unboxing, and VarArgs
  • Meta-data

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Java Comes of Age - Static Import and Generics


    (Page 3 of 6 )

    Static Import

    If you take exception to such lengthy references, in Java1.5 you can now use a static import:


    import static card.Card.CardValue.*;
    import 
    static card.CardSuit.*;

    This makes all the values defined for CardSuit and CardValue available to the importing class as if they were defined in the same class. For example:


    Card sixHearts = new Card(sixhearts);

    Static imports can be used to import static identifiers and static methods as well as enumerations.

    Generics

    Something that I always found irksome about Java was the difference in the strength of typing between arrays and collections. If an array of objects is used to hold some data, then every object in the array has to be a (direct or indirect) instance of the same class. Any attempt to assign an object of some other class to an element of the array results in a compile-time error. On the other hand if a collection is used in Java 1.4 or earlier, then it is a (weakly-typed) collection of objects. This means that assigning an object of some other class to an element of the collection is perfectly acceptable to the compiler, even though it may not have been the intention of the programmer. Such situations give rise to a run-time error (typically a ClassCastException). In other words, the use of collections placed an additional burden on the programmer to avoid run-time errors that could not occur when arrays were used. The programmer was faced with a trade-off between the flexibility of collections and the type-safety of arrays. The good news is that this trade-off no longer exists! From Java 1.5, collections can be strongly typed.

    Instead of saying:


    List deck = new ArrayList();

    and (at most) commenting the intention that the List is to contain only objects of the class Card, we can now enforce that constraint programmatically:


    List<Carddeck = new ArrayList<Card>();

    There is much more to be said about generics than can be covered in this article, but the basic idea should be clear. For more information, take a look at Sun's Generics Tutorial.

    More Java Articles
    More By Simon White


     

       

    JAVA ARTICLES

    - Adding Images With iTextSharp
    - Adding Columns With iTextSharp
    - Creating Simple PDF Files With iTextSharp
    - The Spring Framework: Understanding IoC
    - Introducing the Spring Framework
    - Java Classes
    - Completing the Syntactic Comparison of Java ...
    - Syntactic Comparison of Java and C/C++
    - Java Statements
    - Conditionals, Expressions and Other Java Ope...
    - Java Operators
    - Primitive Data Types and Basic Language Rule...
    - Java and Object-Oriented Programming
    - Java Beginning Programming
    - Gaming Development Setup





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway