This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
I answered first one saying iterator is not synchronized and enumeration is synchronized.
Ugh. What do you think it means for an interface to be synchronized? The answer is "nothing" -- this has no meaning. The difference is that Iterator has (1) shorter method names, and (2) a method for removing the current item. Furthermore, Iterator is newer, and preferred. That's it.
Why do we need ThreadLocal? So that you can have a variable whose value is different in each thread. There are many possible uses for this, not just one reason.
A. Enumeration is a symbolic constant. Also used for the same purpose as an iterator some time in the previous century but I guess that's not relevant, so, symbolic constant. Final answer. An iterator allows you to traverse a collection.
This is where Java shows its Visual Basic roots. Takes a non-thread-safe object (that would be everything and anything in Visual Basic) and creates a thread-safe box by binding a shared static object to a specific thread. Even though it is not thread-safe, each thread gets its own copy. Veebee afficianados call that creating an "apartment". Only one veebee programmer ever understood what the heck that actually meant.
Interview Tip: don't ask to phone a friend or ask the audience if you get a particularly difficult question [ September 02, 2005: Message edited by: Rick O'Shay ]
Joined: Sep 02, 2005
Ernest and Rick, thank you for the replies. It will help me next time. I felt they were difficult questions when I was asked because I didn't prepare for such questions. Rick, your tip is nice
[Rick O'Shay]: A. Enumeration is a symbolic constant.
Ummm... are you referring to an enum? (As the term is used in C/C++.) I think that in this context, "Enumeration" refers to java.util.Enumeration, which is a somewhat different concept from either C/C++ enums, or Java enums. An Enumeration is almost certainly not a symbolic constant.
In my opinion, the differences between java.util.Enumeration and java.land.Iterator are these:
Enumeration has overly verbose method names
Enumeratio9n is totally unnecessary unless dealing with legacy code
Many (not all) implementations of Iterator check (with a small performance cost) against concurrent modifications - which would otherwise be rather difficult to detect. Checking for concurrent modification is well worth the additional effort.
Iterator is easier to use with an enhanced for loop. (Although you usually need ot use an Iterable, not an Iterator - but at least an Iteraor is closer.)
Enumerations are older
Enumerations should be avoided whenever possible
[ September 05, 2005: Message edited by: Jim Yingst ]
"I'm not back." - Bill Harding, Twister
Joined: Jan 30, 2000
[Rick]: Interview Tip: don't ask to phone a friend or ask the audience if you get a particularly difficult question
No - if only because it's useful to know whether you're in an actual interview for a job in the real world, or a contestant on a TV show. I think if you are in an interview, in the real world, it's worthwhile remember that some interview questions are intended to reveal "how do you discover something you don't already know?" rather than "how do you explain something you already know?" In this context, it's worthwhile to be able to describe your strategy for discovering things you don't already know. I would much rather work with someone with a good problem-solving strategy (which often implies a good search strategy), rather than someone who was conviced he (or she) already knew all the answers. [ September 05, 2005: Message edited by: Jim Yingst ]