Q) What is difference between ArrayList and vector?


1) Synchronization – ArrayList is not thread-safe whereas Vector is thread-safe. In Vector class each method like add(), get(int i) is surrounded with a synchronized block and thus making Vector class thread-safe.

2) Data growth – Internally, both the ArrayList and Vector hold onto their contents using an Array. When an element is inserted into an ArrayList or a Vector, the object will need to expand its internal array if it runs out of room. A Vector defaults to doubling the size of its array, while the ArrayList increases its array size by 50 percent.

Q)What is difference between HashMap and HashTable?


Both collections implements Map. Both collections store value as key-value pairs. The key differences between the two are

1. Access to the Hashtable is synchronized on the table while access to the HashMap isn’t. You can add it, but it isn’t there by default.

2. Another difference is that iterator in the HashMap is fail-safe while the enumerator for the Hashtable isn’t. If you change the map while iterating, you’ll know. • Fail-safe – “if the Hashtable is structurally modified at any time after the iterator is created, in any way except through the iterator’s own remove method, the iterator will throw a ConcurrentModificationException”

3. HashMap permits null values and only one null key, while Hashtable doesn’t allow key or value as null.

Introduction to Spring Part II – Bean, BeanFactory, ApplicationContext, IOC


In this technoticle I will be explaining some of the basic concepts of Spring :

Bean – It is the basic element of the framework. It is equivalent to an object created in java. In spring bean can be created using following methods:

1)      Setter injection

2)      Constructor injection

3)      Interface injection

4)      Static factory method

5)      Instance method

The detailed explanation is Dependency Injection

BeanFactory – As the name suggest the beanfactory instantiate, configure and manages all beans defined. These beans typically collaborate with one another, and thus have dependencies between themselves. The ways by which beanFactory can be created: 

  • InputStream is = new FileInputStream(“beans.xml
    XmlBeanFactory factory = new XmlBeanFactory(is);
  •  

  • ClassPathResource res = new ClassPathResource(“beans.xml”); 
    XmlBeanFactory factory = new XmlBeanFactory(res);
  •  

  • ClassPathXmlApplicationContext appContext =  new ClassPathXmlApplicationContext (new String[] { “applicationContext.xml”,”applicationContext-part2.xml”}
    BeanFactory factory = (BeanFactory) appContext;

When any of the above lines are executed all beans present in the “configuration” file(s) are instantiated and stored in the bean factory.Once the bean factory is loaded the bean can be retrieved:

 

   BeanClassName beanObj = (BeanClassName) factory.getBean(“beanName”);

 

ApplicationContext: ApplicationContext extends BeanFactory and thus provide extra functionality than BeanFactory.

The following are the additional functionalities:

·         MessageSource, providing access to messages in, i18n-style

·         Access to resources, such as URLs and files

·         Event propagation to beans implementing the ApplicationListener interface

·         Loading of multiple (hierarchical) contexts, allowing each to be focused on one particular layer, for example the web layer of an application

Invresion of Control – Earlier the control of interface was with the application program. For example it a typical user input application a prompt will pop up saying ‘Enter Name’ or at some place the property has to be set explicitly using the setter methods. But with the advent of IOC, now it’s the responsibility of the container in which beans are loaded to find the correct value of a property with the help of spring configuration file and now not much responsibility is left to the application program to set or get the values.IOC is too generic term, so spring generally designates it as Dependency Injection (DI). 

 A typical IOC container would look like:

IOC Container
IOC Container

 

  • An IOC can have multiple ApplicationContexts.
  • ApplicationContext can also refer other application context.
  • Applicationcontext can contain another applicationcontext.
  • Multiple beanfactories can be inserted in an applicationcontext.

What is volatile keyword?


Ans) In general each thread has its own copy of variable, such that one thread is not concerned with the value of same variable in the other thread. But sometime this may not be the case. Consider a scenario in which the count variable is holding the number of times a method is called for a given class irrespective of any thread calling, in this case irrespective of thread access the count has to be increased. In this case the count variable is declared as volatile. The copy of volatile variable is stored in the main memory, so every time a thread access the variable even for reading purpose the local copy is updated each time from the main memory. The volatile variable also have performance issues.