Monday, March 29, 2010

Diff between transient and volatile variables

Transient variable:
A transient variable is a variable that may not be serialised.

Trying to put a non-serializable variable in a serialisible class,we can use transient modifier ,so that
the jvm skips the transient variable ,and make that class as serializable class

Volatile variable:
The volatile modifier tells the JVM that a thread accessing the variable must always reconcile its own private copy of
the variable with the master copy in memory. It can be apply only to instance variables.
Serialization:

"In computer science, in the context of data storage and transmission, serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file, a memory buffer, or transmitted across a network connection link to be "resurrected" later in the same or another computer environment.[1] When the resulting series of bits is reread according to the serialization format, it can be used to create a semantically identical clone of the original object. For many complex objects, such as those that make extensive use of references, this process is not straightforward." says Wikipedia.

Java provides such a mechanism, called object serialization. A so-called serialized object is an object represented as a sequence of bytes that includes the object’s data as well as information about the object’s type and the types of data stored in the object. After a serialized object has been written into a file, it can be read from the file and deserialized—that is, the type information and bytes that represent the object and its data can be used to recreate the object in memory.

source: http://www.deitel.com/articles/java_tutorials/20050923/IntroductionToObjectSerialization.html

Wednesday, March 24, 2010

Static Keyword in C++

The static keyword specifies that the variable has static duration (it is allocated when the program begins and deallocated when the program ends) and initializes it to 0 unless another value is specified.But, initialize it with 0 if you want to, to avoid confusion
A variable declared static in a function retains its state between calls to that function.
The keyword static can be used in three major contexts: inside a function, inside a class definition, and in front of a global variable inside a file making up a multifile program.

Inside a Function:

The keyword static prevents re-initialization of the variable.
For example:

#include

using namespace std;
void showstat( int curr ) {
static int nStatic = 0;
nStatic += curr;
cout << "nStatic is " << nStatic << endl; }
int main()
{
for ( int i = 0; i < 5; i++ )
showstat( i );
}

Run this once and again run it without static keyword. You'll understand the difference. Inside a class:

To access the static member, you use the scope operator, ::, when you refer to it through the name of the class.
One cannot initialize the static class member inside of the class.
Static member functions can only operate on static members, as they do not belong to specific instances of a class.

Inside a File:

The use of static indicates that source code in other files that are part of the project cannot access the variable. Only code inside the single file can see the variable. (It's scope -- or visibility -- is limited to the file.)

Tuesday, March 23, 2010

The reference type (not the object type) determines which overloaded method is invoked
Reference type determines which overloaded version (based
on declared argument types) is selected. Happens at compile
time. The actual method that’s invoked is still a virtual method
invocation that happens at runtime, but the compiler will
already know the signature of the method to be invoked. So at
runtime, the argument match will already have been nailed
down, just not the class in which the method lives.