Monday, April 14, 2014

what is instaceof in java?

we use instanceof operator to check if an object is an instance of a class, an instance of a subclass,
or an instance of a class that implements a particular interface.It returns true if object is instance of that class
otherwise it returns false.Mostly we use it where we do the the class typecasting.If we use instanceof before typecasting we
will not get the ClassCastException.

for example

Class SuperClass{

}

class Subclass{

}

class MainClass{

public static void main(String [] args){

SuperClass obj1 = new SuperClass();
SubClass obj2 = new SubClass();

if(obj2 instanceof SuperClass){
System.out.println("is obj2 instanceof SuperClass? Ans ="+ obj2 instanceof SuperClass);
obj1 = (SuperClass)obj2;

}


}
}

output : is obj2 instanceof SuperClass? Ans =true

If object is null then instanceof gives always false.

Example

public class InstanceTest{

public static void main(String [] args){

SubClass obj3 =null;
System.out.println("is obj3 instanceof SuperClass? Ans ="+obj3 instanceof SuperClass);

}
}

output : is obj3 instanceof SuperClass? Ans =false

Wednesday, August 28, 2013

How to set value/values(single or multiple) for IN clause in a preparedStatement in JDBC while executing a query.


Suppose we have a table states with following fields

state_id
state_name
country_name

If we want to find the states of india and canada then we will use the prepareStatement as follows

PreparedStatement pstmt = connection.prepareStatement("Select * from states where country_name in (?,?)");
pstmt.setString(1,"india","canada");
result = pstmt.executeQuery();

If we know the number of values which we want to use within in clause then it is very simple as above.
But when we dont know the values which we want to use within in clause then?
when we dont have any idea about the number of values with in IN clause then we will use  prepareStatement as follows.

String queryString = "Select * from states ";

Suppose we have a list of country names(countryNames) which is coming at run time.

StringBuffer sb = new StringBuffer();
for(int i = 0;i<countryNames.size();i++){
sb.append("?,");
}

queryString = queryString + " where country_name IN("+sb.deleteCharAt( sb.length() -1 ).toString()+") ";

pstmt = con.prepareStatement(queryString);
int index = 1;
for( String country : countryNames ) {
   pstmt.setString(  index++, country ); // or whatever it applies
}
result = pstmt.executeQuery();

Thursday, August 1, 2013

class and interface typecasting gotcha!!


 Suppose there are two classes First and Second

 class First{

 }

 class Second{

 }

 class First and Second has no relation(inheritance) and If we write the following statement.

 First obj =  (First)new Second(); // will give error at compile time.

 As all know it will give compile time error.

 But If First(which is class in above case) would be an interface then what would happen?

 Interface First{

 }

 class Second{

 }

 Now If we write the following statement

 First objNew =  (First)new Second(); // will not give compile time error.

 This statement does not give us the compile time error.
 Why this happens in case of interface?

 Reason - In case of Interface there may be a situation that any subclass of Second implements the First interface.Thats why in java type casting of interface is allowed(Because compile is not sure at compile time).

 If we make the class Second final then compiler is sure at compile time that there can't be any subclass of Second the it will give compile time error

 interface First{

 }

 final class Second{

 }

 First objNewer =  (First)new Second(); // will give compile time error 

Thursday, March 14, 2013

What is String Pooling in Java?

When we create string literals jvm maintains all of them in pool known as String pooling.
 for exapmle,

    String str1 = "Hello"; //case 1
    String str2 = "Hello"; //case 2
   
    In case 1, iteral str1 is created newly and kept in the pool. But in case 2, literal str2 refer the str1, it will not create new one instead.
    
       if(str1 == str2) System.out.println("equal"); //Prints equal.
      
       // case 3
      String newString1 = new String("Hello");
      String newString2 = new String("Hello");
      if(newString1 == newString2) System.out.println("equlal"); //No output.
     
      Pooling is done for only string literals. In case 3 it gives no output because both string has a different reference.

Wednesday, March 6, 2013

What is Auto Boxing in Java?

Autoboxing is a new feature offered in the Java SDK 1.5 . In short auto boxing is a capability to convert or cast between object wrapper and it's primitive type.Previously when placing a primitive data into one of the Java Collection Framework we have to wrap it to an object because the collection cannot work with primitive data. Also when calling a method that requires an instance of object than an int or long, than we have to convert it too.

But now, starting from version 1.5 we were offered a new feature in the Java Language, which automate this process, this is call the Autoboxing. When we place an int value into a collection it will be converted into an Integer object behind the scene, on the other we can read the Integer value as an int type. In most way this simplify the way we code, no need to do an explisit object casting.

Here an example how it will look like using the Autoboxing feature:

Conversion of int into Integer and Integer into int

int inative = 0;

inative = new Integer(5); // auto-unboxing

Integer intObject = 5; // autoboxing