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 2007

Tip: Looking for answers? Try searching our database.

Generics question

Thread view: 
sakcee@gmail.com - 30 Apr 2007 22:52 GMT
Hi

what is the difference between following 2 loops?
when is the casting done, i.e. can an object be casted by applying
this generaic 'tags'

thanks for help in advance

Map<String,String> aMap = new HashMap<String,String>();

for(Map.Entry<String,String> e: aMap.entrySet()){
  System.out.print( e.getKey(), e.getValue() );
}

and

for(Map.Entry e: amap.entrySet()){
  System.out.print( e.getKey(), e.getValue() );
}
Daniel Pitts - 30 Apr 2007 22:57 GMT
On Apr 30, 2:52 pm, "sak...@gmail.com" <sak...@gmail.com> wrote:
> Hi
>
[quoted text clipped - 17 lines]
>
> }

The second loop gives you an untyped Map.Entry e.  There is no casting
done. e.getKey()'s signature would appear as an Object, same with
e.getValue().

You are better off using the first version. It specifies exactly what
you want, an Entry with a String key and a String value.
Hendrik Maryns - 02 May 2007 12:23 GMT
sakcee@gmail.com schreef:
> Hi
>
[quoted text clipped - 15 lines]
>    System.out.print( e.getKey(), e.getValue() );
> }

The first is generic, the second isn’t.  But the print() method cares
not.  It just invokes (indirectly) the object’s toString() method, which
both have, generic or not.

It would make more sense if you really did something with the key and
value, e.g. assign them to a local parameter.  Then you’ll see that

Map<String,Integer> aMap = new HashMap<String,Integer>();

for(Map.Entry<String,Integer> e: aMap.entrySet()){
 String key = e.getKey();
 Integer value = e.getValue();
// do some more
}

is allowed, whereas

for(Map.Entry e: amap.entrySet()){
 String key = e.getKey();
 Integer value = e.getValue();
// do some more
}

is not.  To answer your question: generics was introduced to get rid of
all the unnecessary casting.  So there is no casting there.  The
compiler just statically checks that the types are right.

Summary: it is not that casting is done by the generic ‘tags’, but that
the generics /avoid/ casting.

H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html


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



©2009 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.