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.

Wednesday, August 29, 2012

To wait() or to sleep()

The concept of wait and sleep in java threading i couldn't get easily . I read at several tech blogs  and docs that wait should be called by object  while sleep is called by thread . Both methods are used to pause a running thread momentarily . However, i couldn't understand for a long , what does that mean by calling a object ? If wait is to be called  by object then why Thread.currentthread.wait () is visible ? because an object can be of a thread also . 
The thing is a Thread when wants to wait . it should hold an object & use that object to wait  or either it can directly call Thread.sleep(). In Former case object  should be either notify or  be provided  few millisecs to wait . sleep() can be preferred to call when  thread has to wait for small fraction of time .

Both wait() and sleep() cause the current thread to pause execution, not the thread on which you you call them. There is no way to cause a different thread to pause. 

If wait is being  called on a thread then it would give IllegalMonitorStateException .

Tuesday, July 17, 2012

volatile ..java

Use of volatile keyword makes sense when there is a shared object that is being accessed  / modified by multiple threads .Using volatile keyword ensures that variable is never kept in register (stack) for a thread . It will always be read from main memory .
A thread  will never cache the value of a volatile declared variable in its stack space  . so when another thread or by same thread any modification occurs on the volatile variable . its immediately reflected .
However i have not found any running example to show the volatile  affect clearly .

Tuesday, June 26, 2012

WSDL ( in brief )

WSDL is platform- and language-independent and is used primarily to describe SOAP services. It represents the definition of web services . Through  a published WSDL user can create the appropriate soap request to access the publicly available  web service methods. WSDL is written in XML and has 6 major elements .


  • definitions -- root Element
  • types -- data types of all parameters passed to a web method
  • message -- name of the web method that needs to be called or sent in response . its one way only.
  • portType -- it combines multiple messages to form a complete one-way or round-trip operation.
  • binding -- A binding defines message format and protocol details for operations and messages defined by a particular portType.
  • service -- The service element defines the address for invoking the specified service. Most commonly, this includes a URL for invoking the SOAP service.

a sample wsdl can be  seen at below URL
http://www.gotoworkspace.com/us/soap3/timeClockSetupServer.php?wsdl

its respective  soap request for operation (method) named updateTransferJobStatus will be as

< s11:Envelope xmlns:s11='http://schemas.xmlsoap.org/soap/envelope/' >
  < s11:Body >
    < ns1:updateTransferJobStatus xmlns:ns1='urn:gotoworkspaceTimeClock' >
      < clientconfig  /> 

    < tableRowID />
   < recordDetails /> 
  < / ns1:updateTransferJobStatus >
 < / s11:Body >
< / s11:Envelope >
 

and for  operation getPendingJobs corresponding soap request will be 


< s11:Envelope xmlns:s11='http://schemas.xmlsoap.org/soap/envelope/' >

     < s11:Body >
< ns1:getPendingJobs xmlns:ns1='urn:gotoworkspaceTimeClock'>
      < clientconfig />    

  < / ns1:getPendingJobs >  
 < / s11:Body >
< / s11:Envelope >