Wednesday, 24 August 2016

JAVA Features_1

synchronized
It used to achieve thread safeness. Only one thread can execute a method or a block which is declared as synchronized at any given time.
synchronized keyword can be used with methods or blocks but not with the variables, constructors, static initializer and instance initializers.
Some times, you need only some part of the method to be synchronized not the whole method. This can be achieved with synchronized blocks.synchronized block takes one argument and it is called mutex.
synchronized (Shared.class)  {
            //static synchronized block
        }
transient :
This modifier is used in serialization process. A variable which is declared as transient will not be serialized during object serialization.
Auto Winding:
The data is implicitly casted from small sized primitive type to big sized primitive type. This is called auto-widening. i.e The data is automatically casted from byte to short, short to int, int to long, long to float and float to double..
boxing and unboxing.
Wrapping of primitive content into corresponding wrapper class object is called boxing.
Unwrapping the wrapper class object into corresponding primitive content is called unboxing.
int i = 20;       //Primitive int Data
Integer I = i;    //Auto-Boxing of int data
Integer I = new Integer(i);   //Wrapping of primitive int data into Integer Object
int i = I.intValue();           //Unwrapping Integer object to int data
 int i = I;                     //Auto-Unboxing of Integer Object

Marker interface

java are interfaces with no members declared in them. They are just an empty interfaces used to mark or identify a special operation.

some built-in marker interfaces in java which are used to mark some special behavior of a class.

1) java.lang.Cloneable Interface :This interface is used to mark the cloning operation. An object of a class which implements Cloneable interface is eligible for field-by-field copying of an object.

2) java.io.Serializable Interface :This interface is used to mark serialization and deserialization of an object. Serialization is a process in which an object state is read from memory and written into a file or a database.
ClassNotFoundException:
is an exception which occurs when you try to load a class at run time using Class.forName() or loadClass() methods and mentioned classes are not found in the classpath.


Arrays Functions:
Arrays.sort(b);     //sorts elements of the specified array in ascending order
System.out.println(Arrays.binarySearch(i, 55));    
//Output : -7, This method Searches the specified array for the specified value using the binary search algorithm. The array must be sorted before calling this method.

Arrays.fill(i, 10);    //Assigns 10 to each element of the array


Thread creation:
There are two ways to create threads in java language.
1) By extending java.lang.Thread class.
class MyThread extends Thread
{
    @Override
    public void run()
    {
        //Keep the task to be performed here
    }
}

MyThread myThread = new MyThread();
myThread.start();
2) By implementing java.lang.Runnable interface.
class MyThread implements Runnable
{
    @Override
    public void run()
    {
        //Keep the task to be performed here
    }
}
MyThread myThread = new MyThread();    //Creating object of your thread that
Thread t = new Thread(myThread);       //passing your thread object to the
t.start();     






No comments:

Post a Comment