Java
  Home arrow Java arrow Page 5 - Java Comes of Age
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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: starstarstarstarstar / 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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    Java Comes of Age - Auto-boxing, Auto-Unboxing, and VarArgs
    ( Page 5 of 6 )

    Auto-boxing/Auto-Unboxing

    The idea of auto-boxing and auto-unboxing is to make it easier to convert between primitive data types, like int and boolean, and their equivalent Classes, like Integer and Boolean. It is sometimes frustrating to have to do such a conversion, especially if the purpose of the conversion is just for a method call, after which the results must be converted back to their original form again.

    For example, this feature allows you to write the following:


    boolean result Boolean.TRUE && Boolean.FALSE;

    Previously, this would have generated the following error:


    operator && cannot be applied to java.lang.Boolean,java.lang.Boolean
     
            boolean result 
    Boolean.TRUE && Boolean.FALSE;

    With Java 1.5, however, the Booleans are automatically converted to booleans before the && operator is applied.

    The next example shows ints automatically being converted to Integers to store on a Stack, then automatically being converted back again to perform the addition and store the result in the variable stackSum.


    import java.util.Stack;
     
    Stack<IntegermyStack = new Stack<Integer>();
    myStack
    .push(1);
    myStack
    .push(2);
    int stackSum 
    myStack.pop() + myStack.pop();
    System
    .out.println(stackSum);

    VarArgs

    Another new feature is the ability to define methods that accept a variable number of arguments. For example:


    private void printCards(Card ... cards) {
        
    for (Card card cards) {
            System
    .out.println(card.toString());
        
    }
    }

    In a sense, this is syntactic sugar, because the formal parameter to the method is just an array. On the other hand, this is exactly what you would need for a conventional formatted print method, like the printf statement of C. And indeed, Java 1.5 does include such a method! The printCards method above can be rewritten as:


    private void printCards(Card ... cards) {
        
    for (Card card cards) {
            System
    .out.printf("%s of %sn"card.getSuit(), card.getValue());
        
    }
    }

    Here, the printCards method accepts a variable number of arguments and the printf method call also uses a VarArgs method call. The %s in the format control string indicates that a value should be inserted at that point, and the following strings are the strings to be inserted.



     
     
    >>> More Java Articles          >>> More By Simon White
     

       

    JAVA ARTICLES

    - Exception Handling Techniques in Java
    - More About Multithreading in Java
    - The Basics of Multiple Threads in Java
    - Data Access Using Spring Framework JDBC
    - New Object Initialization in Java
    - 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...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT