How can you Earn 30 Dollar Daily ? Click to see the Earning Ways.
Login(Email) Password Forgot Password
Home ASP.net System Info C# Books Java Script Visual C++(MFC) C/C++ Win API Java Contact Us
Browse Category
Java Interview Questions (J2SE, J2EE, J2ME)
What is the main difference between an ArrayList and a Vector? What is the main difference between Hashmap and Hashtable?
Vector / Hashtable ArrayList / Hashmap
Original classes before the introduction of Collections API. Vector & Hashtable are synchronized. Any method that touches their contents is thread-safe So if you don’t need a thread safe collection, use the ArrayList or Hashmap. Why pay the price of synchronization unnecessarily at the expense of performance degradation.
So which is better? As a general rule, prefer ArrayList/Hashmap to Vector/Hashtable. If your application is a multithreaded application and at least one of the threads either adds or deletes an entry into the collection then use new Java collection API‘s external synchronization facility as shown below to temporarily synchronize your collections as needed:

Map myMap = Collections.synchronizedMap (myMap);
List myList = Collections.synchronizedList (myList);

Java arrays are even faster than using an ArrayList/Vector and perhaps therefore may be preferable. ArrayList/Vector internally uses an array with some convenient methods like add(..), remove(…) etc.
What is the main difference between a String and a StringBuffer class?
String StringBuffer
String is immutable: you can’t modify a string object but can replace it by creating a new instance. Creating a new instance is rather expensive.

//Inefficient version using immutable String
String output = “Some text”
int count = 100;
for(int I =0; i<count;i++)
  {
  output += i; 
  }

The above code would build 99 new String objects, of which 98 would be thrown away immediately. Creating new objects is not efficient.
StringBuffer is mutable: use StringBuffer or StringBuilder when you want to modify the contents. StringBuilder was added in Java 5 and it is identical in all respects to StringBuffer except that it is not synchronised, which makes it slightly faster at the cost of not being thread-safe.

//More efficient version using mutable StringBuffer
StringBuffer output = new StringBuffer(110);
Output.append(“Some text”);
for(int I =0; i < count; i++)
{ output.append(i);
}
return output.toString();

The above code creates only two new objects, the StringBuffer and the final String that is returned. StringBuffer expands as needed, which is costly however, so it would be better to initilise the StringBuffer with the correct size from the start as shown.
Another important point is that creation of extra strings is not limited to ‘overloaded mathematical operators’ (“+”) but there are several methods like concat(), trim(), substring(), and replace() in String classes that generate new string instances. So use StringBuffer or StringBuilder for computation intensive operations, which offer better performance
What is a daemon thread?
Daemon threads are sometimes called "service" threads. These are threads that normally run at a low priority and provide a basic service to a program or programs when activity on a machine is reduced. An example of a daemon thread that is continuously running is the garbage collector thread. This thread is provided by the JVM.
What is the order of method invocation in an applet?
public void init() : Initialization method called only once by the browser.
public void start() : Method called after init() and contains code to start processing. If the user leaves the page and returns without killing the current browser session, the start () method is called without being preceded by init ().
public void stop() : Stops all processing started by start (). Done if user moves off page.
public void destroy() : Called if current browser session is being terminated. Frees all resources used by the applet.
How will you communicate between two Applets?
All the applets on a given page share the same AppletContext. We obtain this applet context as follows:

AppletContext ac = getAppletContext();

AppletContext provides applets with methods such as getApplet(name), getApplets(),getAudioClip, getImage, showDocument and showStatus().
What is the difference between an applet and an application?
Applet Application
Applets don’t have a main method. They operate on life cycle methods init(), start(), stop(), destroy() etc.
Applets can be embedded in HTML pages and downloaded over the Internet. Has a sand box security model.
Can only be executed within a Java compatible container like browser, appletviewer etc.
Has a static main() method.
Has no support for embedding or downloading. Has no inherent security restriction
Applications are executed at command line by java.exe.