Sunday, 15 January 2017

Preemptive scheduling vs. time slicing?

In preemptive scheduling, the highest priority task executes until it enters the waiting or dead states or a higher priority task comes into existence. 

In time slicing, a task executes for a predefined slice of time and then reenters the pool of ready tasks. 


The scheduler then determines which task should execute next, based on priority and other factors

What is context-switching in multi-threading?

Context Switching is the process of storing and restoring of CPU state so that Thread execution can be resumed from the same point at a later point of time. Context Switching is the essential feature for multitasking operating system and support for multi-threaded environment.

What is volatile keyword in Java?

When we use volatile keyword with a variable, all the threads read its value directly from the memory and doesn’t cache it. This makes sure that the value read is the same as in the memory.

Thursday, 12 January 2017

Why does Java have transient fields?

The transient variables are never serialized during the Serialization process   and initialize with default value during Deserialization process.

The below example simplify the transient keyword uses. In this example we are consider the id field in Employee is transient variable. In this below example before Serialization Employee object value [id=100, name=Sitansu, Address=BBSR] and After Serialization the Employee object   [id=0, name=Sitansu, Address=BBSR]. This example shows the id field after Serialization assign to default value to 0.


package Java91.blogspot.in;

import java.io.Serializable;

public class Employee implements Serializable{
      
       private static final long serialVersionUID = 1L;
       private transient int id=100;
       private String name;
       private String Address;
      
       public Employee(int id,String name,String Address){
              this.id=id;
              this.name=name;
              this.Address=Address;
       }

       public int getId() {
              return id;
       }

       public void setId(int id) {
              this.id = id;
       }

       public String getName() {
              return name;
       }

       public void setName(String name) {
              this.name = name;
       }

       public String getAddress() {
              return Address;
       }

       public void setAddress(String Address) {
              this.Address = Address;
       }

       @Override
       public String toString() {
              return "Employee [id=" + id + ", name=" + name + ", Address=" + Address + "]";
       }

}

package Java91.blogspot.in;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class DemoTransientTest {
         public static void main(String args[]) {
               
                Employee narnia = new Employee(100,"Sitansu","BBSR");
              System.out.println("Before Serialization: " + narnia);
            
               try {
                   FileOutputStream fos = new FileOutputStream("narnia.ser");
                   ObjectOutputStream oos = new ObjectOutputStream(fos);
                   oos.writeObject(narnia);

                   System.out.println("Employee details is successfully Serialized ");

                   FileInputStream fis = new FileInputStream("narnia.ser");
                   ObjectInputStream ois = new ObjectInputStream(fis);
                   Employee oldNarnia = (Employee) ois.readObject();
                
                   System.out.println("Employee details successfully created from Serialized data");
                   System.out.println("Employee details after seriazliation : " + oldNarnia);
                
               } catch (Exception e) {
                   e.printStackTrace();
               }

           }

}


Output:
Before Serialization: Employee [id=100, name=Sitansu, Address=BBSR]
Employee details is successfully Serialized
Employee details successfully created from Serialized data

Employee details after seriazliation : Employee [id=0, name=Sitansu, Address=BBSR]

Wednesday, 11 January 2017

Convert double to float in java.

There are no on the way to convert the Double to Float.

Just cast your double to a float.
double d = getInfoValueNumeric();
float f = (float)d;
Also, notice that the primitive types can NOT store an infinite set of numbers:
float range: from 1.40129846432481707e-45 to 3.40282346638528860e+38
double range: from 1.7e–308 to 1.7e+308

1) You can achieve it by using decimal formatter .

 double d = 2.342565656767E13;
     DecimalFormat decimalFormat = new DecimalFormat("#");
     System.out.println(decimalFormat.format(d));

2)You can also achieve it by Cast it .
double d = 2.342565656767E13;
float f = (float)d;

What is the best way of using Arrays.asList() to initialize a List.

  1. Arrays.asList(temp)
    It takes an array ia and creates a wrapper that implements List<String>, which makes the original array available as a list. Nothing is copied and all, only a single wrapper object is created. Operations on the list wrapper are propagated to the original array. This means that if you shuffle the list wrapper, the original array is shuffled as well, if you overwrite an element, it gets overwritten in the original array, etc. Of course, some List operations aren't allowed on the wrapper, like adding or removing elements from the list, you can only read or overwrite the elements.
    Note that the list wrapper doesn't extend ArrayList - it's a different kind of object. ArrayLists have their own, internal array, in which they store their elements, and are able to resize the internal arrays etc. The wrapper doesn't have its own internal array, it only propagates operations to the array given to it. On the other hand, if you subsequently create a new array as
  2. new ArrayList<String>(Arrays.asList(temp))
    then you create new ArrayList, which is a full, independent copy of the original one. Although here you create the wrapper using Arrays.asList as well, it is used only during the construction of the new ArrayList and is garbage-collected afterwards. The structure of this new ArrayListis completely independent of the original array. It contains the same elements (both the original array and this new ArrayList reference the same integers in memory), but it creates a new, internal array, that holds the references. So when you shuffle it, add, remove elements etc., the original array is unchanged.

Singleton design pattern in Java

Singleton design pattern

package swain.singleton.javainterviewhub.blogspot.in.designpattern.singleton;

public class SingleObject {

       public static SingleObject singleton = new SingleObject();

       private SingleObject() {

       }

       public static SingleObject getInstance() {
              return singleton;
       }
      
       public void showMessage(){
              System.out.println("hello world");
       }

}


package swain.singleton.javainterviewhub.blogspot.in.designpattern.singleton;

public class SingletonPatternDemo {

       public static void main(String[] args) {
              SingleObject singleObject=SingleObject.getInstance();
              singleObject.showMessage();
       }

}

There are three cases singleton class fail
1) Serialization :We can create multiple instance by searialization case.
2) Multi threading :Singleton class not thread safe
3) Class loader: We can create multiple instance with different class loader