INSERT INTO TableName DEFAULT VALUES
Thursday, August 16, 2007
Inserting rows with default values in a Table
The following simple query will insert a row into the table name specified with the default values for each column:
Tuesday, August 7, 2007
JAVA 5.0 Autoboxing/Unboxing
What is Boxing?
Converting a primitive type to it's wrapper class is known as Boxing. For example converting from int to Integer, float to Float, double to Double etc.
What is unboxing?
The reciprocal of Boxing is called unboxing, means converting from some wrapper class to it's respective primitive type is known as unboxing. For example converting from Integer to int, Float to float, Double to double etc.
In the earlier versions from JDK 1.5 , boxing and unboxing was manual. You have to explicitly convert from one into another. But in JDK 1.5 boxing and unboxing is automatic and the compiler will do this itself on your behalf.
Example of Manual Boxing/unboxing:
int a = 2;
int b;
Integer aW = Integer(a); // This is example of boxing
b =aW.intValue(); // This is an example of unboxing.
If you want to store int values of some range say 0-100 in an ArrayList, then what you need to do ? Certainly you will make the wrapper objects of Integer class and will add them into an array list like here
Converting a primitive type to it's wrapper class is known as Boxing. For example converting from int to Integer, float to Float, double to Double etc.
What is unboxing?
The reciprocal of Boxing is called unboxing, means converting from some wrapper class to it's respective primitive type is known as unboxing. For example converting from Integer to int, Float to float, Double to double etc.
In the earlier versions from JDK 1.5 , boxing and unboxing was manual. You have to explicitly convert from one into another. But in JDK 1.5 boxing and unboxing is automatic and the compiler will do this itself on your behalf.
Example of Manual Boxing/unboxing:
int a = 2;
int b;
Integer aW = Integer(a); // This is example of boxing
b =aW.intValue(); // This is an example of unboxing.
If you want to store int values of some range say 0-100 in an ArrayList, then what you need to do ? Certainly you will make the wrapper objects of Integer class and will add them into an array list like here
ArrayList list = new ArrayList();
for(int i = 0; i <100;i++)
list.add(new Integer(i));// You can pass only Objects into add method // not primitives
}
This is basically boxing. Similarly if you want to add the elements of the ArrayList into some int variable then you need to do the unboxing like shown here:
sum += ((Integer)list.get(i)).intValue();
But if you are using JAVA 5.0, all of these will be done automatically by the compiler on your behalf like shown here:
ArrayList list = new ArrayList();
for(int i = 0; i <100;i++)
{
list.add(i); // AutoBoxing is done here
}
int sum = 0;
for ( Integer j : list)
{
sum += j; // Auto-unboxing is done here..
}
For further reference Read the SUN DOCUMENTATION from the following link:
http://java.sun.com/j2se/1.5.0/docs/guide/language/autoboxing.html
Monday, August 6, 2007
JAVA 5.0 Enhanced For- Loop
Enhanced FOR-LOOP is a new addition in JAVA 5.0. If you have worked in Visual Basic then you have used FOR-EACH loop in VB. Enhanced For-loop in JAVA5.0 is very similar to For-Each Loop of Visual Basic.
Enhanced for-loop is used to iterate arrays and collections.
Iteration over Arrays :
int sumArray( int [] arr )
{
int sumArray=0;
for ( int i=0;i
{
sumArray+=arr[i];
}
return sumArray;
}
If we want to utilize the new Enhanced for-loop( sometimes called For-Each loop or For-In loop) the above function will be like.
int sumArray( int [] arr )
{
int sumArray=0;
for ( int i : arr )
{
sumArray+=i; // here i gets successively each value in array a
}
return sumArray;
}
Iteration over Collections :
Restrictions of Enhanced For-Loop:
Enhanced for-loop is used to iterate arrays and collections.
Iteration over Arrays :
The syntax for iteration over arrays is as following :First we make a function that uses simple/conventional For Loop to add an array of int and returns its sum:
for (type variable : array)
{
body-of-loop
}
int sumArray( int [] arr )
{
int sumArray=0;
for ( int i=0;i
{
sumArray+=arr[i];
}
return sumArray;
}
If we want to utilize the new Enhanced for-loop( sometimes called For-Each loop or For-In loop) the above function will be like.
int sumArray( int [] arr )
{
int sumArray=0;
for ( int i : arr )
{
sumArray+=i; // here i gets successively each value in array a
}
return sumArray;
}
Iteration over Collections :
The syntax for iteration over collections is as following :
for (type variable : collection )
{
body-of-loop
}
For example we have a collection of books in a List and we retrieve it using Iterators using the
conventional for- loop like:
List books = ......
for (Iterator i = books.iterator(); i.hasNext(); )
{
Book book= (Book) i.next();
//..... body of loop ....
}
Its reciprocal syntax using enhanced-for loop will look like,
// Assume there is a List of books available like
List books = ......
for (Book book : books)
{
// /now book has the value for each iteration of list
}
Restrictions of Enhanced For-Loop:
- It is not possible to traverse more than one structures at the same time. For example two arrays.
- You can only access only single element at each iteration. You can not have access to more than one elements. Like in case of comparing successive elements in an array.
- You can not iterate an array backwards. It is forward-only and single step increment.
- It is not compatible for the earlier versions of the JAVA 5.0.
Subscribe to:
Posts (Atom)