Java Techies- Solution for All
File Handling Interview Questions
1 - What is transient variable and need in java?
Transient variable is used to prevent any object from being serialized and you can make any variable transient by using transient keyword. Variables marked transient to indicate that they are not part of the persistent state of an object.2 - What is markable interface in java?
Markable interface is a blank interface with no field and method, example Serializable, Clonnable and Remote. Implementing marker interface indicates jvm to treat class accordingly. JVM checks instanceOf internally, and it find true then only it allow object to serialize , clonnnable etc.3 - What is the difference between Reader/Writer and InputStream/Output Stream?
Use to read/write characters from file. | Use to read/write bytes from file. |
Only read/write text file that contain only characters. | Read/write file that contains image, videos, audios, text etc |
Classes used by Character Streams are FileReader and FileWriter that extends InputStreamReader and OutputStreamReader abstract classes respectively. | Classes used by Byte Streams are InputStream and outputStream both are abstract classes that is extends by the FileInputStream and FileOutputStream respectively. |
4 - Which methods used in serialization and deserialization?
The class implement Serializable interface to persist the object into file. The ObjectOutputStream class writeObject(object) method is use to write objects to file.The class implements Deserializable interface to reconstruct the objects that are in serialized state. This is reverse process of serializable. . The ObjectInputStream class readObject() method is use to read objects from file.
5 - What is Serializable? What is the purpose of Serializable in java?
Serializable is a empty interface with no field and method inside it. Serialization is a mechanism by which you can save or transfer the state of an object by converting it to a byte stream. Class implements Serializable interface to persist class object to file or transfer object over network. The object conversion is done by the JVM using its default serialization mechanism.6 - What is serialVersionUID? What would happen if you don't define this?
The serialization runtime associates with each serializable class a version number, called a serialVersionUID, which is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization.Syntax -
ANY-ACCESS-MODIFIER static final long serialVersionUID = 22L;
serialVersionUID is a static final field. You can assign any number to it. serialVersionUID is essential in serialization process. If developers not add it to java source file, serialization runtime will generate a serialVersionUID and associate it with the class.
7 - Can we transfer a Serialized object via network?
Yes you can transfer a Serialized object via network because java serialized object remains in form of bytes which can be transfer via network. You can also store serialized object in Disk or database.8 - Which kind of variables is not serialized during Java Serialization?
transient and static variable is not serialized during Java Serialization.9 - Difference between Serializable and Externalizable in Java Serialization.
This a markable interface with no field and method. | This contains two methods readExternal() and writeExternal(). |
This interface provides a inbuilt serialization mechanism | this interface is designed to give you control over the serialization mechanism. |
This interface provides a default serialization mechanism | This interface instead of relying on default Java Serialization provides flexibility to control this mechanism. |
10 - What is the alternative to Serialization?
You have other way to persist or transfer object, they are:- Saving object state to database, use ORM tool like hibernate to save and read objects from database.
- XML based data transfer, a lot of XML based web services use this mechanism to transfer data over network. Also lot of tools save xml file to persist data.
- JSON Data Transfer is recently popular data transfer format and inherent integration with web browser due to JavaScript format.