Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Tiger Stripes: Get Ready to Purr Over J2SE 5.0 : Page 2

Tiger, the next version of the J2SE SDK, features language improvements, better performance, better debugging and management, near-native GUI representation, and much more. We grab the Tiger by the tail and take a good look at what it has to offer.


advertisement
Ease of Development
Many of the language changes are designed to improve developer productivity. These include the addition of generics (similar to C++ templates), metadata, autoboxing, enhanced looping, enumerated types, static imports, variable arguments, and many more. You can see a lot of these in detail in JSR-201, JSR-175, and JSR-14.
  • Generics
    Support for generics has become an important area of language competition lately. Microsoft has added it to .NET 2.0, which has just entered beta, while Sun is quickly adding it to Java in version 5. In short, generics is about allowing you to abstract common functionality across multiple data types without rewriting that functionality for each type that you use.

    For example, the following code is used today:

    ArrayList lstList = new ArrayList(); lstList.add(0, new Integer(42)); int total = ((Integer)list.get(0)).intValue();

    In this case, the developer has to understand that list will contain Integers, and use the (Integer) cast when pulling something out. Type mismatches cannot be picked up at compile time as the ArrayList holds Object types, so there would be no type mismatch problems. Referring to the wrong type when pulling information out of the list could lead to an ugly run-time error.

    When using generics, your code would look something like this:

    ArrayList lstList = new ArrayList(); lstList.add(0,new Integer(42)); int total = list.get(0).intValue();

    The use of the generic means that the type of the list is now declared using <Integer> so its types will be strongly checked at compile time. This means that your code is less prone to casting problems or type mismatches at run time, which can only be a good thing!


  • Metadata
    This feature gives you the facility to describe your Java classes, interfaces, methods, and fields with data annotations that can be read by the compiler or other tools and can be used to aid in the integration of your code with other systems. For example, an IDE can query the metadata associated with one of your classes to understand what the class is, and potentially generate code from that. In other words, you could, for example build a Web service bean or EJB and attribute this as being a Web service. Theoretically an IDE could 'detect' that it is a Web service from the metadata and automatically generate stubs and proxies for calling that Web service, eliminating manual steps.


  • Auto-Boxing of Data Primitives
    While this may not get the attention that generics and metadata are getting, this feature is amazingly useful, particularly for new developers (although it might make computer scientists a little skittish). It is simply an automatic, compile-time, transition between simple data types and their object-oriented big brothers. For example, with auto-boxing you can interchange int and Integer without breaking a sweat. This has, in the past, led to a lot of unnecessary coding and casting, and should now make code more readable. Those who love to question prospective job candidates about the difference between boolean and Boolean during interviews will have to look somewhere else for their trick questions!

    Consider the listings above used to demonstrate generics. As the list was defined as being a list of Integer types, when adding to the list you had to cast the value '42' (which is an int) to an Integer for it to be accepted.


    lstList.add(0, new Integer(42));

    with auto-boxing, java is smart enough to know what you want when you use

    lstList.add(0, 42);

    making your code a whole lot more readable.


  • Enhanced Looping
    There is a new construct for looping that certainly makes your code shorter, although it is debatable as to whether or not it makes your code more readable. It removes the concept of an explicit iterator when coding a loop.

    Example:

    For (Iterator i = lstList.iterator(); i.hasNext();) { Integer value = (Integer)i.next; Do Whatever

    } Now becomes:

    For (Integer i : lstList) { Do whatever }

    This kind of looping is nice because it logically places what you are interested in into the loop, instead of having to sidestep into Iterators. On the downside, the construct (Integer I : list) is a little obscure and may take a little getting used to.


  • Enumerated Types
    Long a beloved feature of C++, this is now available in Java so that you can have an enumeration of a data type instead of a long list of constants.

    Public enum maritalStatus{ single, married, divorced, widowed };

    Instead of:

    Static Final STATUS_SINGLE = 0; Static Final STATUS_MARRIED = 1; etc

  • Variable Arguments
    This language enhancement helps you write overloads for functions that accept many different parameter sets. And as you might imagine, it is particularly useful when you have lots of different parameter sets. For example, if you want the same function to have instances that accept 1, 2, or 3 parameters, you don't have to write three overloads). It uses the '…' notation to accept the argument.

    For example:

    int nGetTotalVotes(Object … nStates) { for(int i=0; i<nStates.length; i++) { nTot = nTot + nStates.countVotes(); } }

    Would allow you to call this function with calls like:

    X = nGetTotalVotes( State[FLORIDA], State[WASHINGTON]);

    Or:

    X = nGetTotalVotes( State[NY], State[CA], State[MA], State[NH]);

    Where the list of parameters can change drastically without you having to worry about major overloading headaches.




Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap