Wednesday, August 29, 2007

Java 1.4: ASSERTIONS

Assertions provide a convenient mechanism for verifying that a class's methods are called correctly. This mechanism can be enabled or disabled at runtime. The intention is that assertions typically be enabled during development and disabled in the field.

Assertions has the following syntax:

assert Expression1;
assert Expression1 : Expression2;

  • Expression1 must be boolean type.
  • Expression2 may have any type.
  • If assertions are disables at runtime (default state) then assert statement does nothing.
  • If assertions are enabled at runtime( using command line argument to JVM) then Expression1 is evaluated. If it is true, no further action is taken. If it is false then AssertionError is thrown. If Expression2 is present, it is passed to constructor of the AssertionError where it is converted to String and used as a error message.

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:

INSERT INTO TableName DEFAULT VALUES

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

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 :
 The  syntax for iteration over arrays is as following :

for (type variable : array)
{
body-of-loop
}
First we make a function that uses simple/conventional For Loop to add an array of int and returns its sum:

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.
For more details visit the following link of SUN DOCOMENTATION

Friday, August 3, 2007

JAVA: A Method to Get Current Day of Week

Hello everybody, here is the method that returns the current day of week.

public static String getCurrentDayofWeek()
{
String DAY="";
Calendar cal =Calendar.getInstance();
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
switch (dayOfWeek)
{
case 1:
DAY="SUNDAY";
break;
case 2:
DAY="MONDAY";
break;
case 3:
DAY="TUESDAY";
break;
case 4:
DAY="WEDNESDAY";
break;
case 5:
DAY="THRUSDAY";
break;
case 6:
DAY="FRIDAY";
break;
case 7:
DAY="SATURDAY";
break;
}
return DAY;
}

Thursday, August 2, 2007

New Features in JAVA 5.0

The following is the list of new features added on in version 5.0 of JAVA.
  1. Generics
  2. Enhanced "for" loop ( Click Here for Detail )
  3. Autoboxing/Unboxing ( Click Here for Detail )
  4. Typesafe Enums
  5. Varargs
  6. Static Import ( Click Here for Detail )
  7. Metadata( Annotations)

SCJP Hand outs Part-1

  1. The floating-point numbers did not throw divide by zero ArithmeticException. They will give a result that is Not A Number NAN.
  2. The case argument must be either an int literal , or an int-compatible variable which is a constant (i.e static or final)
  3. Assume byte a=3; byte b=2; byte c= a+b; à Compile time error because the addition operator will wide the byte to int before addition and result should be stored in int variable.
  4. An abstract class can have a constructor.
  5. It is legal to access a static method using an instance of the class. But it is preferred way to use the class name to access.
  6. Methods can be overridden Attributes/member variables can not.
  7. If a try/catch block calls System.exit() then finally block will not execute.
  8. A member variable can not be native.
  9. Constructors are not inherited.
  10. You can not automatically/implicitly convert a char to short. There is same bit depth but since char are unsigned they might have higher positive value than a short can accept.
  11. Arithmetic Exception is not a checked exception.
  12. An interface can extend multiple interfaces however a class can extend only one class.
  13. A member variable can not be declared synchronized.
  14. Math.round(-3.22) = -3
  15. You can not make a final abstract class.
  16. A constructor of a class can not be declared static.
  17. Inner classes can not be defined in a class outside of its methods like where declaring member variables.
  18. A class can not be declared transient.
  19. A method can not be declared transient.
  20. static methods of a class can not be accessed using this.MethodName().
  21. Only variables can be declared as volatile.
  22. A floating point literal (like 3.45) in java will be treated as double unless you specify an f at the end like (3.45f).
  23. A top level class can not be marked as private.
  24. An overloaded method can change the return type, but return type alone is not enough—it also must change the argument list.
  25. If there is a return statement in try/catch block then finally block will also be executed.
  26. int iArray[5] = new int[] {1,2,3,4,5}; will not compile but int iArray[] = new int[] {1,2,3,4,5}; will compile.
  27. An anonymous inner class can not have a constructor.
  28. Result of System.out.println(Math.sqrt(-4)); is equal to NAN.
  29. A top level class can not be marked as protected.
  30. cast is not a keyword in JAVA.