aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes map question from practice exam Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "map question from practice exam" Watch "map question from practice exam" New topic
Author

map question from practice exam

Rachel Glenn
Ranch Hand

Joined: Oct 24, 2012
Posts: 95
Here is the sample code:



I understand that the call to m2.get("a") returns NULL and the call to m2.get(2) returns "b".

I am surprised it is even allowed to compile. The answer says a warning is generated, but I guess the compiler sees that since a raw map is being passed in, that all type info is lost in the actual mangle function?
Greg Charles
Bartender

Joined: Oct 01, 2001
Posts: 2539
    
  10

I see obvious compile errors at lines 5,6, and 11. Line 11 is an obvious typo, but I'm less sure what you're going for on lines 5 and 6.

The thing to remember about generics is they are a compile time concept. If you use a raw type, the compiler will warn you, but won't error out. The runtime won't even care ... at least, not until you try to use an object as a type it's not.
Rachel Glenn
Ranch Hand

Joined: Oct 24, 2012
Posts: 95
Greg Charles wrote:I see obvious compile errors at lines 5,6, and 11. Line 11 is an obvious typo, but I'm less sure what you're going for on lines 5 and 6.

The thing to remember about generics is they are a compile time concept. If you use a raw type, the compiler will warn you, but won't error out. The runtime won't even care ... at least, not until you try to use an object as a type it's not.


yes I do have some typos...but I see what you are saying. Your response made things clear for me in that if we do use the typed collections (as in Map<T>), the compiler will do type checking for us and issue errors if needed. But if we do NOT use the typed collections, but rather untyped version, like a plain old Map, then the most the compiler can do is issue warnings.
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 289
Let me type up the code :

 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: map question from practice exam
 
Similar Threads
MasterExam question
Help understanding a generics and collections question
Generics are going to be the death of me.
Generics and Colletions
more generics