Home arrow Java arrow What`s New in Java 1.5 Tiger?
JAVA

What`s New in Java 1.5 Tiger?


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

Author Info:
By: O'Reilly Media
Rating: 4 stars4 stars4 stars4 stars4 stars / 13
May 19, 2005
TABLE OF CONTENTS:
  1. · What`s New in Java 1.5 Tiger?
  2. · Using Queues
  3. · Ordering Queues Using Comparators
  4. · Overriding Return Types
  5. · Taking Advantage of Better Unicode
  6. · Adding StringBuilder to the Mix

print this article
SEARCH DEVARTICLES

What`s New in Java 1.5 Tiger?
(Page 1 of 6 )

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


blog comments powered by Disqus
JAVA ARTICLES

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- 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 ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials