-
What happens when a thread cannot acquire a lock on an object?
If a thread attempts to execute a synchronized method or synchronized statement and is unable to acquire an object's lock, it enters the waiting state until the lock becomes available.
-
What classes of exceptions may be caught by a catch clause?
A catch clause can catch any exception that may be assigned to the Throwable type. This includes the Error and Exception types.
-
What is the purpose of finalization?
The purpose of finalization is to give an unreachable object the opportunity to perform any cleanup processing before the object is garbage collected.
-
What is synchronization and why is it important?
With respect to multithreading, synchronization is the capability to control the access of multiple threads to shared resources. Without synchronization, it is possible for one thread to modify a shared object while another thread is in the process of using or updating that object's value. This often causes dirty data and leads to significant errors.
-
What are synchronized methods and synchronized statements?
Synchronized methods are methods that are used to control access to an object. A thread only executes a synchronized method after it has acquired the lock for the method's object or class. Synchronized statements are similar to synchronized methods. A synchronized statement can only be executed after a thread has acquired the lock for the object or class referenced in the synchronized statement.
-
How to make a class or a bean serializable?
By implementing either the java.io.Serializable interface, or the java.io.Externalizable interface. As long as one class in a class's inheritance hierarchy implements Serializable or Externalizable, that class is serializable.
-
How many methods in the Externalizable interface?
There are two methods in the Externalizable interface. You have to implement these two methods in order to make your class externalizable. These two methods are readExternal() and writeExternal().
-
What is the difference between Serializalble and Externalizable interface?
When you use Serializable interface, your class is serialized automatically by default. But you can override writeObject() and readObject()two methods to control more complex object serailization process. When you use Externalizable interface, you have a complete control over your class's serialization process.
-
Connecting to a Database and Strings Handling
Constructing a StringIf you are constructing a string with several appends, it may be more efficient to construct it using a StringBuffer and then convert it to an immutable String object.StringBuffer buf = new StringBuffer("Initial Text");// Modifyint index = 1;buf.insert(index, "abc");buf.append("def");// Convert to stringString s = buf.toString();Getting a Substring from a Stringint start = 1;int...
-
Searching a String
String string = "aString";// First occurrence.int index = string.indexOf('S'); // 1// Last occurrence.index = string.lastIndexOf('i'); // 4// Not found.index = string.lastIndexOf('z'); // -1
-
Replacing Characters in a String
// Replace all occurrences of 'a' with 'o'String newString = string.replace('a', 'o');Replacing Substrings in a Stringstatic String replace(String str,String pattern, String replace) {int s = 0;int e = 0;StringBuffer result = new StringBuffer();while ((e = str.indexOf(pattern, s)) >= 0) {result.append(str.substring(s, e));result.append(replace);s = e+pattern.length();}result.append(str.substring(s));return...
-
How do I instantiate a bean whose constructor accepts parameters using the useBean tag?
Consider the following bean: package bar;public class FooBean {public FooBean(SomeObj arg) {...}//getters and setters here}The only way you can instantiate this bean within your JSP page is to use a scriptlet. For example, the following snippet creates the bean with session scope:&l;% SomeObj x = new SomeObj(...);bar.FooBean foobar = new FooBean(x);session.putValue("foobar",foobar);%> You can...
-
-
-
-
-
-
-
-
Java Interview Questions
Ans