 |
Eye of the Tiger - Generics in J2SE 1.5
Posted by sam_dalton on December 03, 2003 at 04:04 AM | Comments (12)
Once again (actually it seems like an age since the last one), a new incarnation of the J2SE is nearing release. This time there seems to be a lot of completely new language features (that is not to say that previous releases have been insignificant).
Below is a list of major features:
- Generics
- Enhanced for loop
- Autoboxing/unboxing
- Typesafe enums
- Static import
- Metadata
As you can see these are a fair number of totally new (to Java anyway) features, but what do they all mean? Well starting at the top, here is a brief description of Generics:
static void dumpSam(Collection c) {
for (Iterator i = c.iterator(); i.hasNext(); ) {
String s = (String) i.next();
if (s.indexOf("sam") > 0)
System.out.println(s);
}
}
The above code dumps the items of a collection that contain the word "sam" to the screen using 1.4 syntax. Nothing much wrong with it, except that the cast is ugly, and there is the potential of a ClassCastException at runtime. Now if we look at how this might be done usign generics, we see that is is much prettier, and also guaranteed not to fail at runtime:
static void dumpSam(Collection<String> c) {
for (Iterator<String> i = c.iterator(); i.hasNext(); ) {
String s = i.next();
if (s.indexOf("sam") > 0)
System.out.println(s);
}
}
We can see from the signature of the method (static void dumpSam(Collection<String> c)) that the collection must only contain Strings, and also from the iterator that it will only return Strings. If you try to pass in a collection containing anything else, the program will simply fail to compile.
Now this may all look a little alien to start with, but I am sure once we get used to seeing generics it will actually be far clearer than code written with casts. What's more, you can try out generics now by downloading the prototype implementation of the spec.
I will blog about some of the other new Java 1.5 features at a later date (once I have had a chance to play with them.)
Bookmark blog post: del.icio.us Digg DZone Furl Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment
-
Confused about erasure
Is your example taken from the JDK 1.5 Collection class?
Presumably the new Collection class will need to be defined as Collection for your example to work.
However if this is the case then its add method will resolve to add(Object varname) after erasure which will cause a compile time conflict with the add(Object varname) method that presumably they will have to leave in there for backwards compatibility....
Posted by: c_armstrong on December 03, 2003 at 06:56 AM
-
Confused about erasure
You can get the above example to work by declaring your collection thus:
ArrayList l = new ArrayList();
This will not allow you to add anything to you collection that is NOT a String (doing so will result in a compile time error).
Posted by: sam_dalton on December 03, 2003 at 07:04 AM
-
Make it shorter ;)
static void dumpSam(Collection c)
{
for (String str : c )
{
if (s.indexOf("sam") > 0)
System.out.println(s);
}
}
Posted by: krage on December 03, 2003 at 07:14 AM
-
Erasure
I'll try and make my question simpler.
If you define Collection
then
what will the add method of the Collection be erased to during compilation?
Posted by: c_armstrong on December 03, 2003 at 07:28 AM
-
Hey all, some mistakes
I think krage's code should look like this
static void dumpSam(Collection c)
{
for (String s : c )
{
if (s.indexOf("sam") > 0)
System.out.println(s);
}
}
Posted by: jeremyzacker on December 03, 2003 at 11:02 AM
-
Hey all, some mistakes (pt .deux)
Damn, I hit the submit before I meant too.
Anways for generics, c_armstrong said that the new Collection class woud look 'need to be defined as Collection '.
Why does it need to extend Object? Why can't T be an interface? I may be wrong, but I'm pretty sure Generics allow interface as well as object declarations.
Posted by: jeremyzacker on December 03, 2003 at 11:06 AM
-
Interface
Jeremy,
Well if you know of an Interface that every Object extends, that would do ;) Otherwise we need Object so that the generic can be parameterised with anything...
Posted by: c_armstrong on December 03, 2003 at 11:52 AM
-
Erasure
Hi,
Where are you defining this? Keep in mind that when you download the new Generics compiler, you get the collection library, already genericized.
When the generics library was compiled, the type parameters were erased.
When your code is compiled, the type parameters in your code will be erased as well (and casts inserted). Compiling your code doesn't change the collections library. So
private ArrayList foo;
will become an ArrayList, and when your code operates on foo, the appropriate casts will be inserted into your code.
Did I answer the question?
Bill
Posted by: wgrosso on December 04, 2003 at 07:36 AM
-
Hey all, some mistakes (pt .deux)
It's just Collection.
The bytecode that gets generated during compilation is exactly what you want.
Bill
Posted by: wgrosso on December 04, 2003 at 07:36 AM
-
Erasure
Bill,
I probably ought to download the 1.5 stuff and look at it.
It was your article on erasure that raised my question.
Specifically the poiint that if two methods erase to the same signature then a subtle compile time error will be generated.
I guess then I am wondering how they compiled the new generics as for example the add(T) method will erase to add(Object) which will clash with the method already there....
C
Posted by: c_armstrong on December 10, 2003 at 05:25 AM
-
wow power leveling
wow powerleveling
wow power leveling
wow gold
wow items
feelingame.com
wow tips
Most Valuable WOW Power Leveling Service
wow power leveling faq
cheap wow power leveling
wow power leveling
wow powerleveling
wow power lvl
Posted by: wowleveling3 on December 13, 2007 at 09:37 PM
-
网络营销软件
网络营销软件
网络营销软件
群发软件
群发软件
---
群发软件
网络营销软件
论坛群发软件
网站排名软件
群发软件
推广小助手破解版
论坛群发软件
网站排名软件
群发软件
推荐给你很好的群发软件和信息群发软件和供求群发软件
推荐给你很好的群发软件和信息群发软件和供求群发软件博客群发软件网络营销软件网络营销软件
网站排名软件网站排名软件网站优化软件信息群发软件信息群发软件信息群发软件论坛群发软件网站推广软件网站推广软件博客群发软件博客群发软件
群发软件群发软件博客群发软件论坛群发软件网络营销软件论坛群发软件
信息群发软件推广软件网站推广软件网络营销软件网站推广软件群发软件网站排名软件网站推广软件博客群发软件论坛群发软件群发软件网站排名软件网站推广软件博客群发软件论坛群发软件
网站排名软件
博客群发软件
网站排名软件
网站推广软件
群发软件信息群发软件
免费论坛群发软件
论坛群发软件
网站排名软件
免费博客群发软件
网站推广软件
群发软件
博客群发软件
网站排名软件
网站推广软件
群发软件信息群发软件
免费论坛群发软件
论坛群发软件
网站排名软件
免费博客群发软件
博客群发软件
信息群发软件
论坛群发软件
信息群发软件
博客群发软件
qq群发软件
邮件群发软件
博客群建软件
企业名录搜索软件
信息群发软件
邮件群发软件
论坛群发软件
博客群发软件
网站推广软件
网络营销软件
全能营销破解版
网络营销软件
论坛群发软件
论坛群发软件
论坛群发软件
网络营销软件
信息群发软件
信息群发软件
信息群发软件
群发软件
论坛群发软件
群发软件
网络营销软件
网站推广软件
Posted by: u89io98 on December 25, 2007 at 11:15 PM
|