Thursday 7 November 2013

Java Interview Question Part-3


Q- Difference Between Serialization and Externalizable interface?
Serialization is Marker interface which doesnt contains any method.The implemented class just gives the instruction to JVM to behave differently for the implemented class.the main goal of serilaliztaion is persist the state of an object into a stream.
By implementing java.io.Serializable, you get "automatic" serialization capability for objects of your class. No need to implement any other logic, it'll just work. The Java runtime will use reflection to figure out how to marshal and unmarshal your objects.

In earlier version of Java, reflection was very slow, and so serializing large object graphs was a bit of a performance problem. To handle this situation, the java.io.Externalizable interface was provided, which is like java.io.Serializable but with custom-written mechanisms to perform the marshalling and unmarshalling functions (you need to implement readExternal and writeExternal methods on your class).It can be useful when there is a transient variable in your class and the value can be calculated from the object's state.

Q- Write a program in java to find the lenghth of string without using any length or size|().

int findStringLength(String str)
{
char[] elements = str.toCharArray();
int count=0;
for(char c : elements){
count++;
}
return count;
}


Q.what is generic Eraser in java?
When we implement the generic then compiler does these things
1. Type casting if necessary to preserve type safety.
2. Resolve all the cases at compile time. Make safe to run.
3. Generate a bridge to gain polymorphism.

And after this compiler removes the generic type and at runtime it is executed without specific type.
It means bytecode of List is same as bytecode of List.

As per the oracle docs,

Generics were introduced to the Java language to provide tighter type checks at compile time and to support generic programming. To implement generics, the Java compiler applies type erasure to:

Replace all type parameters in generic types with their bounds or Object if the type parameters are unbounded. The produced bytecode, therefore, contains only ordinary classes, interfaces, and methods.
Insert type casts if necessary to preserve type safety.
Generate bridge methods to preserve polymorphism in extended generic types.
Type erasure ensures that no new classes are created for parameterized types; consequently, generics incur no runtime overhead.



Q.If new object is created by thread then where object's attributes are going to be created? stack or heap?

class Myclass{

String str;
MyObject object;

public void m1(){

str = new String("hello");
object = new MyObject();
int i = 10;
}
Suppose now thread A creates object of this class and calls method() then where data, object and i will be created? heap or stack of thread?

Object is allocated in Heap, but reference push to the stack. Also you should know that each thread has his own stack.
All four objects: MyClass, MyObject, String str and "hello" are created in Heap area. In fact there is no way an object can be "created" in stack area. Nevertheless, variable 'i' would be created in the stack of m1().
It does not matter whether "Thread" indirectly creates an object or JVM, objects would always be created in the Heap area only.





No comments:

Post a Comment

Find Duplicate Characters In String using Java

package com.zia.test; import java.util.HashMap; import java.util.Map; import java.util.Set; public class findDuplicateCharacter { /**...