File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes interview questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "interview questions" Watch "interview questions" New topic
Author

interview questions

avneesh singh
Greenhorn

Joined: Sep 02, 2005
Posts: 3
Hi,
I have faced these questions in an interview. Please give your answers.

-Difference between enumeration and iterator
-Why do you need ThreadLocal

I answered first one saying iterator is not synchronized and enumeration is synchronized.

I didn't answer second one. Please help.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Hi,

Welcome to JavaRanch!

Originally posted by avneesh singh:

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.


[Jess in Action][AskingGoodQuestions]
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
Q. Difference between enumeration and iterator.

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.

Q. ThreadLocal?

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 ]
avneesh singh
Greenhorn

Joined: Sep 02, 2005
Posts: 3
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
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[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 suck
  • Enumerations should be avoided whenever possible


  • [ September 05, 2005: Message edited by: Jim Yingst ]

    "I'm not back." - Bill Harding, Twister
    Jim Yingst
    Wanderer
    Sheriff

    Joined: Jan 30, 2000
    Posts: 18671
    [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 ]
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: interview questions
     
    Similar Threads
    ??? Difference Between Iterator & Enumeration ???
    Diff between Enumeration and Iterator
    Implementation of Interfaces
    Enumeration
    Iterator & Enumeration