"Before software can be reusable it first has to be usable"- Ralph Johnson

Friday, April 27, 2012

How Can We restrict someone to create a limited objects of a class

By 12:05 AM
Here is example how we can restrict someone to create a limited objects of a class :-

class Example{ 
    private static int count; 
    private static int MAX_COUNT = 5; 
    private Example(){ 
    } 
      
    public static Example getObject(){ 
        if(count < MAX_COUNT){ 
              count ++; 
              return new Example
(); 
        } 
        else{
            System.out.println("You exceded the limit");
            //TO DO
        } 
    }
Read More...

Wednesday, February 8, 2012

Top Java Collection Interview Questions

By 8:52 AM
1) What is the Collections API?
A: The Collections API is a set of classes and interfaces that support operations on collections of objects.

2) What is the List interface?
A: The List interface provides support for ordered collections of objects.

3) What is the Vector class?
A: The Vector class provides the capability to implement a growable array of objects.  

4) What is an Iterator interface?
A: The Iterator interface is used to step through the elements of a Collection .

5) Which java.util classes and interfaces support event handling?
A: The EventObject class and the EventListener interface support event processing.

6) What is the GregorianCalendar class?
A: The GregorianCalendar provides support for traditional Western calendars

7) What is the Locale class?
A: The Locale class is used to tailor program output to the conventions of a particular geographic, political, or cultural region .

8) What is the SimpleTimeZone class?
A: The SimpleTimeZone class provides support for a Gregorian calendar .

9) What is the Map interface?
A: The Map interface replaces the JDK 1.1 Dictionary class and is used associate keys with values.

10) What is the highest-level event class of the event-delegation model?
A: The java.util.EventObject class is the highest-level class in the event-delegation class hierarchy.

11) What is the Collection interface?
A: The Collection interface provides support for the implementation of a mathematical bag, an unordered collection of objects that may contain duplicates.

12) What is the Set interface?
A: The Set interface provides methods for accessing the elements of a finite mathematical set. Sets do not allow duplicate elements.

13) What is the typical use of Hashtable?
A: Whenever a program wants to store a key value pair, one can use Hashtable.

14) What is the difference between the size and capacity of a Vector?
A: The size is the number of elements actually stored in the vector, while capacity is the maximum number of elements it can store at a given instance of time.

15) Can a vector contain heterogenous objects?
A: Yes a Vector can contain heterogenous objects. Because a Vector stores everything in terms of Object.
Read More...

Important Java Collection Interview Questions & Answers

By 8:36 AM
What is difference between Set and List in Java?
Most of Java programmer knows that Set doesn't allowed duplicate while List does and List maintains insertion order while Set doesn't. What is key here is to show interviewer that you can decide which collection is more suited based on requirements.
 
Difference between HashMap and Hashtable?
HashMap is not synchronized while hashtalbe is not or hashmap is faster than hashtable etc. What could go wrong is that if he placed another followup question like how hashMap works in Java or can you replace Hashtable with ConcurrenthashMap etc. See HashTable vs HashMap in Java for detailed answer of this interview question.
 
What is difference between Synchronized Collection and ConcurrentCollection?
Java5 has added several new ConcurrentCollection classes e.g. ConcurrentHashMap, CopyOnWriteArrayList, BlockingQueue etc, which has made Interview questions on Java Collection even trickier. Java Also provided way to get Synchronized copy of collection e.g. ArrayList, HashMap by using Collections.synchronizedMap() Utility function.One Significant difference is that ConccurentCollections has better performance than synchronized Collection because they lock only a portion of Map to achieve concurrency and Synchronization.
 
What is difference between fail-fast and fail-safe Iterators?
Fail-fast Iterators throws ConcurrentModificationException when one Thread is iterating over collection object and other thread structurally modify Collection either by adding, removing or modifying objects on underlying collection. They are called fail-fast because they try to immediately throw Exception when they encounter failure. On the other hand fail-safe Iterators works on copy of collection instead of original collection

What is difference between Iterator and Enumeration?
Iterator duplicate functionality of Enumeration with one addition of remove() method and both provide navigation functionally on objects of Collection.Another difference is that Iterator is more safe than Enumeration and doesn't allow another thread to modify collection object during iteration except remove() method and throws ConcurrentModificaitonException.

When do you use ConcurrentHashMap in Java?
ConcurrentHashMap is better suited for situation where you have multiple readers and one Writer or fewer writers since Map gets locked only during write operation. If you have equaled number of reader and writer than ConcurrentHashMap will perform in line of hashtable or synchronized hashMap.

How do you Sort objects on collection?
Sorting is implemented using Comparable and Comparator in Java and when you call Collections.sort() it gets sorted based on natural order specified in CompareTo method while Collections.sort(Comparator) will sort objects based on compare() method of Comparator.
Read More...

Types of Garbage Collector in Java

By 8:13 AM
Java Runtime(J2SE 5) provides various types of Garbage collection in Java which we can choose based upon our application's performance requirement. J2SE 5 adds three additional garbage collectors except serial garbage collector. Each is generational garbage collector which has been implemented to increase throughput of the application or to reduce garbage collection pause times.
 
1) Throughput Garbage Collector: This garbage collector in Java uses a parallel version of the young generation collector. It is used if the -XX:+UseParallelGC option is passed to the JVM via command line options . The tenured generation collector is same as the serial collector.


2) Concurrent low pause Collector: This Collector is used if the -Xingc or -XX:+UseConcMarkSweepGC is passed on the command line. This is also referred as Concurrent Mark Sweep Garbage collector. The concurrent collector is used to collect the tenured generation and does most of the collection concurrently with the execution of the application. The application is paused for short periods during the collection. A parallel version of the young generation copying collector is sued with the concurrent collector. Concurrent Mark Sweep Garbage collector is most widely used garbage collector in java and it uses algorithm to first mark object which needs to collected when garbage collection triggers.


3) The Incremental (Sometimes called train) low pause collector: This collector is used only if -XX:+UseTrainGC is passed on the command line. This garbage collector has not changed since the java 1.4.2 and is currently not under active development. It will not be supported in future releases so avoid using this and please see 1.4.2 GC Tuning document for information on this collector.


Important point to not is that -XX:+UseParallelGC should not be used with -XX:+UseConcMarkSweepGC. The argument passing in the J2SE platform starting with version 1.4.2 should only allow legal combination of command line options for garbage collector but earlier releases may not find or detect all illegal combination and the results for illegal combination are unpredictable. It’s not recommended to use this garbage collector in java.
Read More...