Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2005

Tip: Looking for answers? Try searching our database.

them 5.0 language additions

Thread view: 
opalpa@gmail.com - 23 May 2005 23:14 GMT
Peeked at
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Collections.html, 5.0
javadoc for collections and it has stuff like:

binarySearch(List<? extends Comparable<? super T>> list, T key)

This used to be simple:

binarySearch(List list, Object key, Comparator c)

Generics have made code a bit simpler to write, my opinion anway.
However mkaing the javadoc look like C++ code is not to my liking.

<? looks like an html formating error, but I doubt it.
Wibble - 24 May 2005 01:57 GMT
Angle brackets smelled in C++, and java smells like C++ but funnier.

> Peeked at
> http://java.sun.com/j2se/1.5.0/docs/api/java/util/Collections.html, 5.0
[quoted text clipped - 10 lines]
>
> <? looks like an html formating error, but I doubt it.
Wayne - 24 May 2005 02:06 GMT
> Peeked at
> http://java.sun.com/j2se/1.5.0/docs/api/java/util/Collections.html, 5.0
[quoted text clipped - 10 lines]
>
> <? looks like an html formating error, but I doubt it.

It's not a formatting error.  There are several short, readable
intoductions to generics with the JDK documentation and elsewhere.
Please note you don't have to use 'em!  The old way works fine.
The new way is better and usually leads to less (and more readable)
code, but it is one more thing you need to learn.

Try these links:

  Overview of new J5 features:
    http://java.sun.com/features/2003/05/bloch_qa.html
    http://java.sun.com/j2se/1.5.0/docs/relnotes/features.html

  Generics:
    http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf

Hope this helps!

-Wayne
Wibble - 24 May 2005 04:08 GMT
I like generics, just think the syntax is ugly and hard to read.
What can ya do...

>> Peeked at
>> http://java.sun.com/j2se/1.5.0/docs/api/java/util/Collections.html, 5.0
[quoted text clipped - 29 lines]
>
> -Wayne
opalpa@gmail.com - 25 May 2005 03:11 GMT
Thanks Wayne, that's a few pages to read there, will do, will do.   Can
you translate the above mentioned 5.0 specification of

binarySearch(List<? extends Comparable<? super T>> list, T key)

to a couple of english sentances?

Like, looking for something of type T.... and looking in a list that
has.... what in it?  not just things of type T.... The things in the
list are....

They are question marks.... ok... and that is an interface because
Comparable is an interface.... So oh darn.... why is The comparabale
got <? super T>.... I get lost...
Wayne - 25 May 2005 06:26 GMT
> Thanks Wayne, that's a few pages to read there, will do, will do.   Can
> you translate the above mentioned 5.0 specification of
[quoted text clipped - 10 lines]
> Comparable is an interface.... So oh darn.... why is The comparabale
> got <? super T>.... I get lost...

Well I'm hardly an expert but I'll try to explain.  (If I get it
wrong someone will yell at me and then you'll know the right
answer. :-)

Short answer:

BinarySearch method takes a (sorted) list of Comparable items,
and an item to search for.

Less short answer:

The questionmark is a kind of wild-card.  This is needed because
List<Object> is *not* a supertype of List<String>, despite that it
looks like it should be.  From the Generics PDF:

    List<String> ls = new ArrayList<String>();
    List<Object> lo = ls;  // error

The second line won't compile!  You need to use a "List of any type":

    List<?> lo = ls;

For binarySearch, List<?> as an argument would be too general.  You
only want lists of Comparables (or types that extend Comparables).
Apparently to avoid introducing new keywords in Java, they came
up with this ugly syntax:

    List<? extends Comparable>

This means a list of anything that implements or extends Comparable.
So given:

    class Foo {}
    class Bar extends Foo {}

Then I can pass a list of Bars to a method expecting: List<? extends Foo>
(Technically this is called a "bounded wildcard".)

The syntax:

    List<? super Bar>

is similar.  It defines a list of objects of type Bar, or
any superclass of Bar.  (This is called a "lower bound".)  Examine
the following code and note the lines marked as giving compiler
errors, hopefully they should make sense to you:

==================

import java.util.*;

class Foo implements Comparable<Foo>
{
   public int compareTo ( Foo obj ) { return 0; }
}

class Bar extends Foo {}

public class GenericsTest
{

   private static Foo foo = new Foo();
   private static Bar bar = new Bar();

   public static void main ( String [] args )
   {
      baseTest();
      extendsTest();
      superTest();
   }

   static void baseTest ()
   {
      List<Foo> fooList = new ArrayList<Foo>();
      List<Bar> barList = new ArrayList<Bar>();

      fooList.add( foo );
      fooList.add( bar );
//      barList.add( foo );  // error
      barList.add( bar );

      Collections.binarySearch( fooList, foo);
      Collections.binarySearch( fooList, bar);
      Collections.binarySearch( barList, foo);
      Collections.binarySearch( barList, bar);
   }

   static void extendsTest ()
   {
      List<? extends Foo> fooList = new ArrayList<Foo>();
      List<? extends Bar> barList = new ArrayList<Bar>();

//      fooList.add( foo );  // error
//      fooList.add( bar );  // error
//      barList.add( foo );  // error
//      barList.add( bar );  // error
   }

   static void superTest ()
   {
      List<? super Foo> fooList = new ArrayList<Foo>();
      List<? super Bar> barList = new ArrayList<Bar>();

      fooList.add( foo );
      fooList.add( bar );
//      barList.add( foo );  // error
      barList.add( bar );
   }
}

==============   

Final issue:

binarySearch ( List<? extends Comparable>, XXX key )

What should "XXX" (the type of parameter "key") be?  The answer is that
it should be the same type as the items in the list.  To say I have a
method that takes a list of any type and an object of that same type
requires the use of "generic methods".  The full declaration for
binarySearch in Java5 is:

    public static <T> int binarySearch (
        List<? extends Comparable<? super T>> list,
        T key
    )

The "<T>" says this is a generic method, where "T" is some type whenever
it is seen in the declaration.  (The same type all the time.)

Now we can read this:  java.utils.Collections.binarySearch is a public
static method that takes two arguments.  The first is a List of Comparable
items of some type T (or any supertype of T), and a "key" of type T.

Hope this helps a bit!

-Wayne
opalpa@gmail.com - 25 May 2005 22:36 GMT
Thank you.  That's many bits worth of help.  Now I comprehend.
John Maline - 25 May 2005 19:52 GMT
There was a good article at http://onjava.com covering exactly this kind
of stuff.
http://www.onjava.com/pub/a/onjava/excerpt/javaian5_chap04/index1.html

> Thanks Wayne, that's a few pages to read there, will do, will do.   Can
> you translate the above mentioned 5.0 specification of
[quoted text clipped - 10 lines]
> Comparable is an interface.... So oh darn.... why is The comparabale
> got <? super T>.... I get lost...


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.