Java Interview Question
Categories: Java 8(JDK1.8)
What is the transient keyword?
If you define any data member as transient, it will not be serialized. By determining transient keyword, the value of variable need not persist when it is restored.
What is Externalizable?
The Externalizable interface is used to write the state of an object into a byte stream in a compressed format. It is not a marker interface.
What is the difference between Serializable and Externalizable interface?
No.SerializableExternalizable
1)The Serializable interface does not have any method, i.e., it is a marker interface.The Externalizable interface contains is not a marker interface, It contains two methods, i.e., writeExternal() and readExternal().
2)It is used to "mark" Java classes so that objects of these classes may get the certain capability.The Externalizable interface provides control of the serialization logic to the programmer.
3)It is easy to implement but has the higher performance cost.It is used to perform the serialization and often result in better performance.
Give a brief description of Java socket programming?
Java Socket programming is used for communication between the applications running on different JRE. Java Socket programming can be connection-oriented or connectionless. Socket and ServerSocket classes are used for connection-oriented socket programming and DatagramSocket, and DatagramPacket classes are used for connectionless socket programming. The client in socket programming must know two information:
a) IP address of the server
b) port number
What is Socket?
A socket is simply an endpoint for communications between the machines. It provides the connection mechanism to connect the two computers using TCP. The Socket class can be used to create a socket.
What are the steps that are followed when two computers connect through TCP?
There are the following steps that are performed when two computers connect through TCP.
a) The ServerSocket object is instantiated by the server which denotes the port number to which, the connection will be made.
b) After instantiating the ServerSocket object, the server invokes accept() method of ServerSocket class which makes server wait until the client attempts to connect to the server on the given port.
c) Meanwhile, the server is waiting, a socket is created by the client by instantiating Socket class. The socket class constructor accepts the server port number and server name.
The Socket class constructor attempts to connect with the server on the specified name. If the connection is established, the client will have a socket object that can communicate with the server.
What is the reflection?
Reflection is the process of examining or modifying the runtime behavior of a class at runtime. The java.lang.Class class provides various methods that can be used to get metadata, examine and change the runtime behavior of a class. The java.lang and java.lang.reflect packages provide classes for java reflection. It is used in:
a) IDE (Integrated Development Environment), e.g., Eclipse, MyEclipse, NetBeans.
b) Debugger
c) Test Tools, etc.
What is the purpose of using java.lang.Class class?
The java.lang.Class class performs mainly two tasks:
a) Provides methods to get the metadata of a class at runtime.
b) Provides methods to examine and change the runtime behavior of a class.
What are the ways to instantiate the Class class?
There are three ways to instantiate the Class class.
a) forName() method of Class class: The forName() method is used to load the class dynamically. It returns the instance of Class class. It should be used if you know the fully qualified name of the class. This cannot be used for primitive types.
b) getClass() method of Object class: It returns the instance of Class class. It should be used if you know the type. Moreover, it can be used with primitives.
c) the .class syntax: If a type is available, but there is no instance then it is possible to obtain a Class by appending ".class" to the name of the type. It can be used for primitive data type also.