Friday, 31 October 2008

Java concepts

4.  JVM with version
version 4 - Autoboxing (wrapping primitive to objects automaticaly) and Unboxing
Version 5 - Generic/Templating, Iterator, You can create array by using triple dot ...
  - static import,  import static 
Version 6 - No Idea yet
Version 7 - Closure(Functions inside functions and functions will have a reference)

7. Serialization (Usages)
1. persistence 
2. copy mechanism
3. communication mechanism

8. Cloning: 
Deep and sallow cloning:

Thought: while doing deep cloning, copy of the object might we can not copy all the objects so easly,  so serilize it and deserilize it
   How to serilize and deserialize and object?
   Sample code:
Contact  myObject = new Contact();
System.out.println("Previous: "+myObject);
ByteArrayOutputStream fos=new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(myObject);
oos.flush();
oos.close();
byte[] array=fos.toByteArray();
ByteArrayInputStream fis=new ByteArrayInputStream(array);
ObjectInputStream ois = new ObjectInputStream(fis);
Contact myDeserializedObject = (Contact)ois.readObject();
ois.close();
System.out.println("After: "+myDeserializedObject);

   
9. How to make a class immutable?
1. make all variable private
2. no setter methods
3. in getter method, two things should be there
1. either we return the copy of the class variables
2. or we return a an object of class which is immutable again, like string
4. in constructor also we don't assign class variable directly to passed variables, it should copy the content

Sample questions on String immutability
String s1="abc";
String s2="abc";
s1.equals(s2), true
s1==s2, true
String s3=new String("abc");
s1.equals(s3), true
s1==s3, false

10. Why do we need to extends RuntimeException class?
Answer is consistency in approach, 
There are two basic type of exception, application exception(checked) and system exceptions (unchecked) So when we feel that some exception need not to reach to use, we'd like to wrap that in a system exception. There should be two base Exception classes, one for each and should be use whenever relevent exceptions are thrown. When SQLException is thrown, we wrap it in a custom system exception and throw it, note: checked is been converted in to unchecked.

11. Can we declare a class as static?
Top level calsses can not be declared as static, compiler error.
Inner classes can be, and it act as a static object of outer class. Static inner classes imply that the object of the inner class is not dependent upon the outer class object and can exist indepenently.
Now there are four types of Inner classes.
1. Anonymous: since Anonymous classes have not got proper class declration,     no possibility of making it static
2. Local : Local classes are the same as local variables, local classes aren't allowed to be declared public, protected, private, or static.
3. Member: The member class is the only class that you can declare static. When you declare a member class, you can instantiate that member class only within the context of an object of the outer class in which this member class is declared. If you want to remove this restriction, you declare the member class a static class.
4. Nested top-level. A nested top-level class is a member classes with a static modifier. A nested top-level class is just like any other top-level class except that it is declared within another class or interface.
Post a Comment