Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Generics and Maps

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am preparing for SJCP 1.5 and I read Complete Java 2 certification by Philip Heller and Simon Roberts. My understanding is one of the features of Generics clllections is that it will prevent putting the wrong type inside the collction in the first place:

import java.util.*;

class test {
public static void main ( String [] args ) {
Map<Integer, String> map = new LinkedHashMap<Integer, String> ();
Map<Integer, String> sap = new HashMap<Integer, String> ();
populate( map );
populate( sap );
System.out.println( map.get(1) + sap.get(1) );
}

static void populate ( Map m ) {
for ( int i = 0 ; i < 10 ; i++ ) {
m.put(i,i);
}
}
}

However, this code does the opposite. It defines the map will take Integer and String only. But in the popuate methid it puts two integers instead of Integer and String. How does it work? I am assuiming that compiler boxes the first i to Integer. I cannot reason our the second argument. Can some one help. Thanks.
Raj
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Raj

This is an example where the Generics code(1.5 and higher) has been mixed with legacy code.

In the method populate(Map map), note that it is not using the Generics syntax which means that you can add whatever type you want into this map. So, the compiler will not complain at all in the populate(Map map) method.

The problem is actually while retrieving the values from the map that is in the System.out.println(map.get(1) + sap.get(1)). As it will throw an ClassCastException at run-time.

The reason is that map and sap reference variables are declared as Generics type i.e., Map<Integer,String> map = new LinkedHashMap<Integer,String>. At run-time, when you try to retrieve then it throws a ClassCastException indeed.

If you change the System.out.println() statement as follows:

System.out.println( String.valueOf(map.get(1)) + String.valueOf(sap.get(1)) );

then you will see the output as '11'.

Hope this helps.

Best Regards
Kris
 
Krishnamoorthy Vuyala Muralidharan
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Raj

The most important thing about Generics is that it exists only at compile-time and not at run-time. When you compile java source-code then the Compiler removes all the Generics code. The main reason for this is to support the legacy code(non-generic code).

If you open the byte-code (.class) file, you will not find any Generics syntax at all.

Best Regards
Kris
 
Marshal
Posts: 69856
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . but welcome to JavaRanch, Raj Jawahar.
 
A day job? In an office? My worst nightmare! Comfort me tiny ad!
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic