Friday, September 28, 2012

Java Tidbits ........................3 (Serialization)

  • A class that needs to be serialized must implement  marker interface Serializable.
  • A class can extends a base class that doesn't implement Serializable .
  • A class if  contains or has-a relation with any of such  instance variable that doesn't implement Serializable will throw runtime exception while serializing . [ ..java.io.NotSerializableException ..]
  • If the base class doesn't implement Ser then in that case  while deserializing object the constructor of base class is called again while the transient and static variables of object will be intialized to default and recent vale respectively .
Try running below example 

import java.io.*;

class Player {
Player() { System.out.print("p"); }
}
public class CardPlayer  extends Player implements Serializable{
// Player p =new Player (); this can be commented to check Has-A relation ship 
CardPlayer() { System.out.print("c"); }
public static void main(String[] args) {
    CardPlayer c1 = new CardPlayer();
try {
             FileOutputStream fos = new FileOutputStream("play.txt");
             ObjectOutputStream os = new ObjectOutputStream(fos);
             os.writeObject(c1);
             os.close();
            FileInputStream fis = new FileInputStream("play.txt");
            ObjectInputStream is = new ObjectInputStream(fis);
            CardPlayer c2 = (CardPlayer) is.readObject();
            is.close();
} catch (Exception x ) { System.out.println("here ") ; x.printStackTrace();}
}
}

Thursday, September 27, 2012

Java Tidbits ............................2 (Finally)


will finally execute when return / System.exit(0) is invoked in try or catch block?

Yes . with return finally block will execute before returning the value but with System.exit(0) it immediately quits JVM so no point of execution any furthur 

------------------------------------------------------------------------------------------

BEWARE , Finally is not guaranteed to complete ! It all depends on user's implementation.
see below example ::


public class OverAndOver {
 static String s = "";
 public static void main(String[] args) {
 try {
 s += "1";
     throw new Exception();
   } catch (Exception e) { s += "2";
     } finally { s += "3"; doStuff(); s += "4";
     }
 System.out.println(s);
 }
 static void doStuff() { int x = 0; int y = 7/x; }
 }


Wednesday, September 26, 2012

Java Tidbits ............................1

Can one extends multiple interfaces in java ?


interface A {

void doStuffA();
}

interface B {

void doStuffB();
}

interface C extends A,B  {
void doStuffC();
}

// Is C valid interface ?
Yes ! it is because interfaces are not classes ....... 

 -------------------------------------------------------------------------------------------


Monday, September 3, 2012

To Extend or to Implement

This is the most basic question that i never been able to understand until i made a program in which i started a  thread twice .

The answer that novice programmer give is
1) a Java class can have only one superclass. So if your thread class extends java.lang.Thread, it cannot inherit from any other classes. This limits how you can reuse your application logic.

correct !!
Now when same question is asked to an exp programmer then some more reason is expected  .
The reason is

2) Once a thread is  finished with its task ie: comes out of run method and cannot be restarted . Thread instance is subjected to garbage collection. So in order to resubmit the task again at some point then implementing interface is required. Also  the task can be submitted for a later point of execution .


From a design point of view, there should be a clean separation between how a task is identified and defined, between how it is executed. The former is the responsibility of a Runnable impl, and the latter is job of the Thread class.