Java 2 [Collection,multi-threading,Object creation]

What are all the methods used for Inter Thread communication and what is the class in which these methods are defined?

There are three ways in which Threads communicate with each other :

wait: It tells the calling thread to give up the monitor until some other thread enters the same monitor an calls Notify or Notifyall...

notify: It wakes up the first thread that called Wait() on the same object...

NotifyAll: It allows all the threads that called Wait() on the same object.The thread having the highest priority will run first...

Inter thread communication is done using wait and notify (or notify all). These methods are defined in 'Object' class.When Threads are executing on shared resources inorder to establish the communication among them the Object class providing some native methods like wait() notify() notifyAll().


In how many ways we can create an object? Explain with example.

In Java objects can be created in 4 ways.

1. using new operator
2. First get class reference either using Class.forname or class loader. On this class, create object using 'newInstance' method.
3. Clone
4. Deserialization
1. Using new keyword
This is the most common way to create an object in java. I read somewhere that almost 99 of objects are created in this way.

MyObject object new MyObject();

2. Using Class.forName()
If we know the name of the class & if it has a public default constructor we can create an object in this way.

MyObject object (MyObject) Class.forName( subin.rnd.MyObject ).newInstance();

3. Using clone()
The clone() can be used to create a copy of an existing object.

MyObject anotherObject new MyObject();
MyObject object anotherObject.clone();

4. Using object deserialization
Object deserialization is nothing but creating an object from its serialized form.

ObjectInputStream inStream new ObjectInputStream(anInputStream );
MyObject object (MyObject) inStream.readObject();

5. Using class loader
one more is through creation of object using classloader
like

this.getClass().getClassLoader().loadClass( com.amar.myobject ).newInstance();
But its advised to create objects only when it is necessary to do so.


What is the meaning of supplying string[] args to main method?

This array of Strings is the mechanism through which the runtime system passes information to your application. Each String in the array is called a command line argument. Command line arguments let users affect the operation of the application without recompiling it. For example, a sorting program might allow the user to specify that the data be sorted in descending order with this command line argument:
-descending.


why java does not support inheritance of multiple superclasses?
what is achieved by inheritance?
why there is no main method in servlets/jsps?


If there will be multiple base classes then the class which is extending from those base classes will be in ambiguity problem this problem is named as Diamond problem.That's why Java doesn't support multiple inheritance.We can create a new class including the properties of the previous class without destroying it.This is possible through inheritance only.
Beans( JAVA CLASSES) are different from web componets ...( Servelts & JSPs)
web components always serves the web requests means just getting the requests and process the request with the help of beans or some times them selves only .
here in Servlets Service () is working like a main ()
all the processing is directed from this funtion only and finally response will sent form this funtion only so it seems to be a main() in Beans .
So may the name is different but the functionality is same for Main() in Beans and Service() in Servlets..

How can we use hashset in collection interface?

This class implements the set interface backed by a hash table (actually a HashMap instance). It makes no guarantees as to the iteration order of the set; in particular it does not guarantee that the order will remain constant over time. This class permits the Null element.

This class offers constant time performance for the basic operations (add remove contains and size) assuming the hash function disperses the elements properly among the buckets. Iterating over this set requires time proportional to the sum of the HashSet instance's size (the number of elements) plus the capacity of the backing HashMap instance (the number of buckets). Thus it's very important not to set the initial capacity too high (or the load factor too low) if iteration performance is important.


What is difference between java.lang .Class and java.lang.ClassLoader? What is the hierarchy of ClassLoader ?

A Class is nothing but a byte code which will be generated when you compile a java file. And it is required to execute your java file. Where as a ClassLoader is also a class which loads the class files into memory in order for the Java programs to execute properly. The hierarchy of ClassLoaders is:

1. Bootstrap ClassLoaders
2. Extensive ClassLoaders
3. System Classpath ClassLoaders
4. Application ClassLoaders or (EAR ClassLoaders)
5. EJB ClassLoaders or (JAR ClassLoaders)
6. WAR ClassLoaders.

What is difference between array & arraylist?

ArrayList is a part of the Collection Framework.We can store any type of objects and we can deal with only objects.It is growable.Array is collection of similar data items.We can have array of primitives or objects.It is of fixed size We can have multi dimensional arrays.

Post a Comment

أحدث أقدم