Expansion-Pune-10 November 2008
Expansion-Pune-10 November 2008
Expansion-Pune-10 November 2008
1. Contracts between equals and hashCode method?
Answer:The general contract of hashCode is:
1- If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
2- Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
2. Is struts action classes thread safe by default?
Answer:No, by default struts action classes are not thread safe
Our controller servlet creates only one instance of your Action class, and uses this one instance to service all requests. Thus, you need to write thread-safe Action classes. Follow the same guidelines you would use to write thread-safe Servlets. Here are two general guidelines that will help you write scalable, thread-safe Action classes:
1-Only Use Local Variables - Local variables are created on a stack that is assigned (by your JVM) to each request thread.
2-Conserve Resources -You should strive to use pools and release resources (such as database connections) prior to forwarding control to the appropriate View component -- even if a bean method you have called throws an exception.
3-Whats difference between comparator and comaprable interface?
Comparable interface has “compareTo” method which is normally used for natural ordering.
When you need natural sort order you can implement the Comparable interface.
Comparator interface has “compare” method which takes two arguments. It can be used where you want to sort objects based of more then one parameter.
If You want an alternate sort order or sorting on different properties then implement a
Comparator for your class.
Code snippet for Difference Between Comparable And Comparator Interface
4) String Pooling?
Answer:String when created using "" representation uses a concept of string pooling in which all same character string thus created holds reference to same object i.e no new object is created but when new String("") constructor is used it creates new object.
for same string created using "" both equals and == are true
for string created using new operator value equality will return true.
5)What pattern is used to implement string pooling concept?
Answer:Flywieght,flyweight is a design pattern that Java itself heavily depends upon.
flyweight pattern is appropriate when "many objects must be manipulated and these cannot afford to have extraneous data." In Java, String objects are managed as flyweight. Java puts all fixed String literals into a literal pool. For redundant literals, Java keeps only one copy in the pool.
You can force a dynamically created string into the literal pool by creating a new String using String's .intern() method.
e.g.
working on a scheduling application that supports appointments. It contains a Time class that encapsulates a time of day, expressed using hour (presuming a 24-hour clock) and minute.
My application must support very large numbers of appointments, perhaps millions, and I'm concerned about the memory requirement. The reality is, however, that appointments during a business day typically start either on the hour, or at quarter hour intervals past the hour. If I use the flyweight pattern so that I only store unique Time instances, I can reduce the maximum number of typical time objects to 96 (24 hours times four possible times per hour). The appointment application will still support odd times, but predictable use suggests that flyweight will dramatically minimize object creation and memory usage.
The key to making flyweight work is by controlling object instantiation using a factory method. The job of a factory method is simply to create objects: given input criteria, return an object of appropriate type.
6) Difference between Factory and AbstractFactory pattern?
Answer:
"Factory Method" is just used to create object of
the same kind (ie. Cats) that have different variations (ie. breeds of
cats).
The difference lies in how dynamic the substitution needs to be. In both
cases the Product and the Factories to produce them are generalizations.
The difference is that in Factory Method the subclasses in each
generalization map 1:1 to each other while in Abstract Factory the
Factory subclasses map 1:* to Product subclasses.
Abstract factory class which contains many factory methods (in other words its dealing with abstractions)
Thus in Factory Method a concrete factory object can instantiate exactly
one flavor of one Product. But in Abstract Factory a single concrete
factory object can instantiate a different flavor of Product from each
of multiple Product generalizations. IOW, one uses Factory Method when
there is only one family of products and one uses Abstract Factory when
there are construction similarities across multiple families of products.
Abstract Factory Usage