Marker interfaces are also called "tag" interfaces since they tag all the derived classes into a category based on their purpose. For example, all classes that implement the Cloneable interface can be cloned (i.e., the clone() method can be called on them). The Java compiler checks to make sure that if the clone() method is called on a class and the class implements the Cloneable interface. For example, consider the following call to the clone() method on an object o:
SomeObject o = new SomeObject();
SomeObject ref = (SomeObject)(o.clone());
If the class SomeObject does not implement the interface Cloneable (and Cloneable is not implemented by any of the superclasses that SomeObject inherits from), the compiler will mark this line as an error. This is because the clone() method may only be called by objects of type "Cloneable." Hence, even though Cloneable is an empty interface, it serves an important purpose.
other marker interfaces are
- java.io.Serializable
- java.util.EventListener
- java.util.RandomAccess
Wednesday, August 11, 2010
Monday, July 19, 2010
Volatile in Java
If, in the following example, one thread repeatedly calls the method one() and another thread repeatedly calls the method two():
class Test {
static int i = 0, j = 0;
static void one() { i++; j++; }
static void two() {
System.out.println("i=" + i + " j=" + j);
}
}
then method two could occasionally print a value for j that is greater than the value of i, because the example includes no synchronization and the shared values of i and j might be updated out of order.
One way to prevent this out-of-order behavior would be to declare methods one and two to be synchronized.
Another approach would be to declare i and j to be volatile:
class Test {
static volatile int i = 0, j = 0;
static void one() { i++; j++; }
static void two() {
System.out.println("i=" + i + " j=" + j);
}
}
This allows method one and method two to be executed concurrently, but guarantees that accesses to the shared values for i and j occur exactly as many times, and in exactly the same order, as they appear to occur during execution of the program text by each thread. Therefore, the shared value for j is never greater than that for i, because each update to i must be reflected in the shared value for i before the update to j occurs
class Test {
static int i = 0, j = 0;
static void one() { i++; j++; }
static void two() {
System.out.println("i=" + i + " j=" + j);
}
}
then method two could occasionally print a value for j that is greater than the value of i, because the example includes no synchronization and the shared values of i and j might be updated out of order.
One way to prevent this out-of-order behavior would be to declare methods one and two to be synchronized.
Another approach would be to declare i and j to be volatile:
class Test {
static volatile int i = 0, j = 0;
static void one() { i++; j++; }
static void two() {
System.out.println("i=" + i + " j=" + j);
}
}
This allows method one and method two to be executed concurrently, but guarantees that accesses to the shared values for i and j occur exactly as many times, and in exactly the same order, as they appear to occur during execution of the program text by each thread. Therefore, the shared value for j is never greater than that for i, because each update to i must be reflected in the shared value for i before the update to j occurs
Monday, July 12, 2010
Static Import
It helps to import all the static members of a class into our class
For e.g. If u want to import all static members of Math class,Then
u can give the import statement like the following:
import static Java.Lang.Math.*;
Basic need of this type of import is u can directly call those static methods without using their class name.
for e.g: u can call absolute function in Math class as
abs(10);
No need to give like this as Math.abs(10);
For e.g. If u want to import all static members of Math class,Then
u can give the import statement like the following:
import static Java.Lang.Math.*;
Basic need of this type of import is u can directly call those static methods without using their class name.
for e.g: u can call absolute function in Math class as
abs(10);
No need to give like this as Math.abs(10);
Wednesday, March 10, 2010
Defining functions with varying parameters
Ellipsis concepts in java helps to define a function with varying parameters.Following example will help u to understand this.
public class VaryingParametersTest {
public static void main(String args[]) {
System.out.println(add(1, 2, 3));
System.out.println(add(1, 2, 3, 4, 5, 6));
}
public static int add(int... values) {
int sum = 0;
for (int val : values) {
sum = sum + val;
}
return sum;
}
}
Saturday, January 23, 2010
Various ways in creating the object
1.Using new operator:
A a=new A();
2.Using Reflections:
A a=Class.forName("A").newInstance();
3.Using clone method:
A a=new A();
A b=a.clone();
Class A should implements cloneable interface
4.Using ClassLoader:
A a =cL.loadClass("A").newInstance();
cL is instance of ClassLoader.
A a=new A();
2.Using Reflections:
A a=Class.forName("A").newInstance();
3.Using clone method:
A a=new A();
A b=a.clone();
Class A should implements cloneable interface
4.Using ClassLoader:
A a =cL.loadClass("A").newInstance();
cL is instance of ClassLoader.
Subscribe to:
Posts (Atom)