Java
  Home arrow Java arrow What`s New in Java 1.5 Tiger?
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
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

What`s New in Java 1.5 Tiger?
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 7
    2005-05-19

    Table of Contents:
  • What`s New in Java 1.5 Tiger?
  • Using Queues
  • Ordering Queues Using Comparators
  • Overriding Return Types
  • Taking Advantage of Better Unicode
  • Adding StringBuilder to the Mix

  • 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


    What`s New in Java 1.5 Tiger?


    (Page 1 of 6 )

    Call it Java 1.5, 2.0, Java 5, Tiger, or what have you -- this version of Java has a lot to offer. This article covers just some of the new features. It is excerpted from chapter one of Java 1.5 Tiger: A Developer's Notebook, written by Brett McLaughlin and David Flanagan (O'Reilly, 2004; ISBN: 0596007388).

    Even with nearly 200 pages before you, it’s going to be awfully tough to cover all of Tiger’s new features. Whether it’s called Java 1.5, 2.0, Java 5, or something altogether different, this version of Java is an entirely new beast, and has tons of meat to offer.

    Rather than waste time on introductory text, this chapter jumps right into some of the “one-off” features that are new for Tiger, and that don’t fit into any of the larger chapters. These will get you used to the developer’s notebook format if it’s new for you, and introduce some cool tools along the way. Then Chapter 2 gets downright serious, as generics are introduced, and from there to the last page, it’s a race to cram everything in.

    Working with Arrays

    Tiger has a pretty major overhaul of its collection classes, most of which have to do with generic types and support for the new for/in loop. Without getting into those details yet, you can get some immediate bang for your buck by checking out the java.util.Arrays class, which is chock-full of static utility methods (many of which are new to Tiger).

    How do I do that?

    The java.util.Arrays class is a set of static methods that all are useful for working with arrays. Most of these methods are particularly helpful if you have an array of numeric primitives, which is what Example 1-1 demonstrates (in varied and mostly useless ways).

    Example 1-1.  Using the Arrays utility class

      package com.oreilly.tiger.ch01;
     
    import java.util.Arrays;
      import java.util.List;
     
    public class ArraysTester {
       
    private int[] ar;
        
    public ArraysTester(int numValues) {
          ar = new int[numValues];
         
    for (int i=0; i < ar.length; i++) {
            ar[i] = (1000 - (300 + i));
          }
        }
       
    public int[] get() {
          return ar;
        }
       
    public static void main(String[] args) {
          ArraysTester tester = new ArraysTester(50);
          int[] myArray = tester.get();
         
    // Compare two arrays
          int[] myOtherArray = tester.get().clone();
          if (Arrays.equals(myArray, myOtherArray)) { 
     
            System.out.println("The two arrays are equal!");
          } else {
            System.out.println("The two arrays are not equal!");
          }
         
    // Fill up some values
          Arrays.fill(myOtherArray, 2, 10, new Double(Math.PI).intValue());
          myArray[30] = 98;
         
    // Print array, as is
          System.out.println("Here's the unsorted array..."); 
          System.out.println(Arrays.toString(myArray)); 
          System.out.println();
         
    // Sort the array
          Arrays.sort(myArray);
         
    // print array, sorted
          System.out.println("Here's the sorted array..."); 
          System.out.println(Arrays.toString(myArray)); 
          System.out.println();
         
    // Get the index of a particular value
          int index = Arrays.binarySearch(myArray, 98); System.out.println("98 is located in the array at index " + index);
         
    String[][] ticTacToe = { {"X", "O", "O"},
                                   {"O", "X", "X"},
                                   {"X", "O", "X"}};
          System.out.println(Arrays.deepToString(ticTacToe));
          String[][] ticTacToe2 = { {"O", "O", "X"},
                                   {"O", "X", "X"},
                                   {"X", "O", "X"}};
          String[][] ticTacToe3 = { {"X", "O", "O"},
                                    {"O", "X", "X"},
                                    {"X", "O", "X"}};
         
    if (Arrays.deepEquals(ticTacToe, ticTacToe2)) {      
          System.out.println("Boards 1 and 2 are equal.");
          } else {
            System.out.println("Boards 1 and 2 are not equal.");
          }
         
    if (Arrays.deepEquals(ticTacToe, ticTacToe3)) {
            System.out.println("Boards 1 and 3 are equal.");
          } else {
          System.out.println("Boards 1 and 3 are not equal.");
          }
        }
      }

    The first method to take note of, at least for Tiger fans, is toString(). This handles the rather annoying task of printing arrays for you. While this is trivial to write on your own, it’s still nice that Sun takes care of it for you now. Here’s some program output, showing the effects of Arrays.toString() on an array:

       Note:  Running Ant and supplying a target of "run-ch01" automates 
              this.                                 

      run-ch01:
          [echo] Running Chapter 1 examples from Tiger: A Developer's Notebook
         
    [echo] Running ArraysTester...
          [java] The two arrays are equal!
         
    [java] Here's the unsorted array...
          [java][700, 699, 3,
    3, 3, 3, 3, 3, 3, 3, 690, 689, 
          688, 687, 686, 685, 684, 683, 682, 681, 680, 679, 
          678, 677, 676, 675, 674, 673, 672, 671, 98, 669, 668,       667, 666, 665, 664, 663, 662, 661, 660, 659, 658, 
          657, 656, 655, 654, 653, 652, 651]
          [java] Here's the sorted array...
         
    [java] [3, 3, 3, 3, 3, 3, 3, 3, 98, 651, 652, 653, 
      654, 655, 656, 657, 658, 659, 660, 661, 662, 663, 664,
      665, 666, 667, 668, 669, 671, 672, 673, 674, 675, 676,
      677, 678, 679, 680, 681, 682, 683, 684, 685, 686, 687,
      688, 689, 690, 699, 700]
    [java] 98 is located in the array at index 8

    Another similar, but also new, method is deepToString(). This method takes in an object array, and prints out its contents, including the contents of any arrays that it might contain. For example:

          String[][] ticTacToe = { {"X", "O", "O"},
                                   {"O", "X", "X"},
                                   {"X", "O", "X"}};
         
    System.out.println(Arrays.deepToString(ticTacToe));

    Here’s the output:

      [java] [[X, O, O], [O, X, X], [X, O, X]]

    This starts to really come in handy when you’ve got three or four levels of arrays, and don’t want to take the time to write your own recursion printing routines.

    Finally, Arrays provides a deepEquals() method that compares multidimensional arrays:

      String[][] ticTacToe = { {"X", "O", "O"},
                               {"O", "X", "X"},
                               {"X", "O", "X"}};
     
    System.out.println(Arrays.deepToString(ticTacToe));
      String[][] ticTacToe2 = { {"O", "O", "X"},
                                {"O", "X", "X"},
                                {"X", "O", "X"}};
     
    String[][] ticTacToe3 = { {"X", "O", "O"},
                                {"O", "X", "X"},
                                {"X", "O", "X"}};
     
    if (Arrays.deepEquals(ticTacToe, ticTacToe2)) {  
        System.out.println("Boards 1 and 2 are equal.");
      } else {
        System.out.println("Boards 1 and 2 are not equal.");
      }
     
    if (Arrays.deepEquals(ticTacToe, ticTacToe3)) {   
        System.out.println("Boards 1 and 3 are equal.");
      } else {
        System.out.println("Boards 1 and 3 are not equal.");
      }

    As expected, the first comparison returns false, and the second true:

      [java] Boards 1 and 2 are not equal.
     
    [java] Boards 1 and 3 are equal.

    What About…

    …hash codes? Java 101 dictates that every good equals() method should be paired with an equivalent hashCode(), and the Arrays class is no exception. Arrays defines both hashCode() and deepHashCode() methods for just this purpose. I’ll leave it to you to play with these, but they are self-explanatory:

      int hashCode = Arrays.deepHashCode(ticTacToe);

    More Java Articles
    More By O'Reilly Media


     

    Buy this book now. This article was taken from chapter one of Java 1.5 Tiger: A Developer's Notebook, written by Brett McLaughlin and David Flanagan (O'Reilly, 2004; ISBN: 0596007388). Check it out at your favorite bookstore. Buy this book now.

    JAVA ARTICLES

    - Deploying Multiple Java Applets as One
    - Deploying Java Applets
    - Understanding Deployment Frameworks
    - Database Programming in Java Using JDBC
    - Extension Interfaces and SAX
    - Entities, Handlers and SAX
    - Advanced SAX
    - Conversions and Java Print Streams
    - Formatters and Java Print Streams
    - Java Print Streams
    - Wildcards, Arrays, and Generics in Java
    - Wildcards and Generic Methods in Java
    - Finishing the Project: Java Web Development ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...







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