Saturday, September 29, 2012

Serialisation - readObject and writeObject


 what is the significance of readObject and writeObject method in Serialization ?
These methods are not declared in any class or interface. A class, that implements the Serializableinterface and requires special special handling during the serialization and deserialization process must implement those methods and the serializer/deserializer will try to reflect those methods.

 These methods are (and must be) declared private (when implementing your own), proving/indicating that neither method is inherited and overridden or overloaded by the implementation. The trick here is that the JVM automatically checks to see if either method is declared during the corresponding method call. Note that the JVM can call private methods of your class whenever it wants but no other objects can. Thus, the integrity of the class is maintained and the serialization protocol can continue to work as normal.



Monday, September 17, 2012

Arrays.asList()


Arrays.asList() is efficient because it doesn't need to copy the content of the array. This method returns a List that is a "view" onto the array - a wrapper that makes the array look like a list. When you change an element in the list, the element in the original array is also changed. Note that the list is fixed size - if you try to add elements to the list, you'll get an exception. If you only need read access to the array as if it is a List and you don't want to add or remove elements from the list, then use Arrays.asList().

new ArrayList(Arrays.asList(myArray)) copies the content of the array to a new ArrayList. The copy is ofcourse independent of the array, and you can add, remove etc. elements as you like.

Collections.addAll(myList, myStringArray) is essentially the same as new ArrayList(Arrays.asList(myArray)) .