May 05 2012

What does a passionate developer do…

Category: English posts,TechnicalIuliana @ 22:35

… when is stuck in a hotel room because of the rain, bored out of his mind and with no mood to work? Well when that happens to me I start to reed my java feed reader and depending on what I find there I might write my own articles. So… yes, this article is today’s consequence of boredom.

1. What is the flaw with the Stack class?
Actually, there are two of them all being caused by the fact that java.util.Stack extends java.util.Vector:
I. Extending Vector methods insertElementAt and removeElementAt can be called and they actually work, so the stack definition is not respected (that part with only the last inserted element being accessible, the LIFO principle)
II. Extending Vector, Stack is also synchronized which makes it slow and when synchronization is not necessary this  is quite inefficient. This is not exactly a flaw, it’s more of a personal observation observation.

Then again in the api it is written that “It extends class Vector with five operations that allow a vector to be treated as a stack “, so I guess these are not flaws, the Stack class just works as intended. (Recommendation: use ArrayDeque)

2. Can an interface extend multiple interfaces?
There is no right answer to this question, because it depends of the point of view of the interviewer.
I. Yes, because you can define an interface like this:
public interface MultipleIntf extends List, Serializable {
}
II. No, extending means actually inheriting all functionality of the super-entity and  perhaps adding new functionality, in the case of interfaces there is nothing to inherit and no functionality to add. Except for the obligation to implement all abstract methods that will be enforced on the implementing class.

3. What is lazy loading?
Lazy loading is a name to describe the process of not loading something (object/class) until needing it. This question will surely take you to a ClassLoader discussion, so it is better to know and understand the Java Class Loading mechanism.
So:
– the java source files are compiled into executable code for the JVM, called bytecode, stored into *.class files.
– at start-up JVM has no loaded classes. When the first class is loaded, the classes on which its execution depends are searched and loaded too. So if I have a class which has imports statement for ArrayList and Serializable, the JVM will load my class, then it will search and load ArrayList.class and Serializable.class. Let’s imagine we have a big application with a lot of class files and one of them is missing. The application will run just fine, until we try to access a functionality implemented by that class, when the JVM will let us know that the class was not found by throwing a java.lang.ClassNotFoundException.
And this my darlings is lazy loading. A class is not loaded until used and there would be no point in doing that for efficiency reasons. Right? Anyway, if you want to have a deep understanding of the Java Class Loader, I recommend this article.

Tags: , , ,

2 Responses to “What does a passionate developer do…”

  1. b.m says:

    seaqxx, a minor correction, you should add a context in here that you refer to class loaders. Lazy loading is a concept used in many places (dependency injection engines come to mind) where beans are proxied and created only when you try to invoke a method in them.

    Basically lazy loading is loading of something on usage. Be it class, creating new instances, etc.

  2. Iuliana says:

    Fixed, thanks!

Leave a Reply