This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes map question from practice exam Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
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
Sheriff

Joined: Oct 01, 2001
Posts: 2840
    
  11

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: 722
Let me type up the code :

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: map question from practice exam
 
Similar Threads
Generics are going to be the death of me.
Help understanding a generics and collections question
Generics and Colletions
more generics
MasterExam question