Previous | Home | Next |
Answer: Wrapper class is wrapper around a primitive data type. An instance of a wrapper class contains, or wraps, a primitive value of the corresponding type.
Following are the lists of the primitive types and the corresponding wrapper classes:
Primitive | Wrapper |
boolean | java.lang.Boolean |
byte | java.lang.Byte |
char | java.lang.Character |
double | java.lang.Double |
float | java.lang.Float |
int | java.lang.Integer |
long | java.lang.Long |
short | java.lang.Short |
void | java.lang.Void |
Answer: Inner classes nest within other classes. A normal class is a direct member of a package. Inner classes, which became available with Java 1.1, are four types:
- Static member classes
- Member classes
- Local classes
- Anonymous classes
-
Static member classes: A static member class is a static member of a class. Like any other static method, a static member class has access to all static methods of the parent, or top-level, class.
-
Member Classes: A member class is also defined as a member of a class. Unlike the static variety, the member class is instance specific and has access to any and all methods and members, even the parent's this reference.
-
Local Classes: Local Classes declared within a block of code and these classes are visible only within the block.
-
Anonymous Classes:These type of classes does not have any name and its like a local class.
Answer: StringBuffer is faster than String for concatenation.
Q.26 What is Runnable interface ? Are there any other ways to make a java program as multithred java program?
Answer: The thread can be create by using two methods:
- Define a new class that extends the Thread class
- Define a new class that implements the Runnable interface, and pass an object of that class to a Thread's constructor.
The advantage of implements the Runnable interface is that the new class can be a subclass of any class, not just of the Thread class.
Answer: Prior to Java 5, isAlive() was commonly used to test a threads state. If isAlive() returned false the thread was either new or terminated but there was simply no way to differentiate between the two.
Starting with the release of Tiger (Java 5) you can now get what state a thread is in by using the getState() method which returns an Enum of Thread.States.
Answer: Java provides three methods that threads can use to communicate with each other: wait, notify, and notifyAll.
Answer: A call to notify causes at most one thread waiting on the same object to be notified (i.e., the object that calls notify must be the same as the object that called wait). A call to notifyAll causes all threads waiting on the same object to be notified. If more than one thread is waiting on that object, there is no way to control which of them is notified by a call to notify (so it is often better to use notifyAll than notify).
Answer: Synchronization is the act of serializing access to critical sections of code. We will use this keyword when we expect multiple threads to access/modify the same data. To understand synchronization we need to look into thread execution manner.
Threads may execute in a manner where their paths of execution are completely independent of each other. Neither thread depends upon the other for assistance. For example, one thread might execute a print job, while a second thread repaints a window. And then there are threads that require synchronization, the act of serializing access to critical sections of code, at various moments during their executions. For example, say that two threads need to send data packets over a single network connection. Each thread must be able to send its entire data packet before the other thread starts sending its data packet; otherwise, the data is scrambled. This scenario requires each thread to synchronize its access to the code that does the actual data-packet sending.
If you feel a method is very critical for business that needs to be executed by only one thread at a time (to prevent data loss or corruption), then we need to use synchronized keyword.
Answer: Serialization is the process of writing complete state of java object into output stream, that stream can be file or byte array or stream associated with TCP/IP socket.
Answer: Serializable is a tagging interface; it prescribes no methods. It serves to assign the Serializable data type to the tagged class and to identify the class as one which the developer has designed for persistence. ObjectOutputStream serializes only those objects which implement this interface.
Answer: To serialize an object into a stream perform the following actions:
- Open one of the output streams, for example FileOutputStream.
- Chain it with the ObjectOutputStream - Call the method writeObject() providing the instance of a Serializable object as an argument.
- Close the streams
Java code:
try{ fOut= new FileOutputStream("c:\\raj.ser"); out = new ObjectOutputStream(fOut); out.writeObject(employee); //serializing System.out.println("An employee is serialized into c:\\emp.ser"); } catch(IOException e) { e.printStackTrace(); }
Answer: To deserialize an object, perform the following steps:
- Open an input stream
- Chain it with the ObjectInputStream - Call the method readObject() and cast the returned object to the class that is being deserialized.
- Close the streams
Java code:
try{ fIn= new FileInputStream("c:\\emp.ser"); in = new ObjectInputStream(fIn); //de-serializing employee Employee emp = (Employee) in.readObject(); System.out.println("Deserialized " + emp.fName + " " + emp.lName + " from emp.ser "); }catch(IOException e){ e.printStackTrace(); }catch(ClassNotFoundException e){ e.printStackTrace(); }
Answer: Externalizable interface is a subclass of Serializable. Java provides Externalizable interface that gives you more control over what is being serialized and it can produce smaller object footprint. This interface defines 2 methods: readExternal() and writeExternal() and you have to implement these methods in the class that will be serialized. In these methods you'll have to write code that reads/writes only the values of the attributes you are interested in. Programs that perform serialization and deserialization have to write and read these attributes in the same sequence.
Previous | Home | Next |