Thursday 24 January 2008

New Java 5 Features - Part 2

Printf
If you have worked on C language then you would be familiar with Printf statement. Now you can use it in java also.

See the below example

package testnewfeatures;

public class Test_PrintfStatement {

public static void main(String[] args) {
int a = 10;
String str = "Pakistan";

System.out.printf("Value of a = %d ", a );

System.out.println();

System.out.printf("How are you ? Welcome to %s ", str );

//For more details of these Formate Specifiers
//see java.util.Formatter


}
}


Variable Arguments

Public void setSomething(Object ... args) {
// . . . . .
}

Var-args
is the way to make one method able to handle more than one type of arguments. In past we were creating separate methods for each argument type.

check out below example.

package testnewfeatures;
// Testing Variable Arguments feature
public class Test_Var_Arg {

public static void main(String[] args) {
String[] names = {"Asim","Faisal","Qasim"};

oldMethod( names );
newMethod( names );
newMethod( "A","B","C","D","E","F","......Z" );

empInfo("Asim","Programmer",15000,1000);

}
// Old (Ver 1.1 - to - 1.4) method of Method Declaration
public static void oldMethod(String[] args) {
for ( String argValue : args) {
System.out.println( argValue );
}
}
// New (Ver 1.5 or Simply Java 5) method of Method Declaration
// It mush be the LAST argument
public static void newMethod(String ... args) {
for ( String argValue : args) {
System.out.println( argValue );
}
}

public static void empInfo(Object ... values) {
Object[] tempObj = {"Name","Designation",10000,500};

System.arraycopy(values,0,tempObj,0,values.length);

System.out.printf("Name = %s \nDesignation = %s \nBasic Salary = %d " +
"\nTax Deduction = %d", tempObj );
}

}


Generics
In a nutshell, generics allows java programmers to pass types as arguments to classes just as values are passed to methods.

String custName = (String) myList.getFirst(); ⁄⁄ without generics
String custName = myList.getFirst(); ⁄⁄ with generics

In the first example above we had to perform casting.
But in the second example we do not require this(casting) anymore. Because when we are going to create an object then we have to specify what type of object it will handle/support.

LinkedList<> myList = new LinkedList &ltString>();

See the examples given below.

Example 1:-

package testnewfeatures;
import java.util.Vector;
import java.util.Enumeration;
public class TestGenerics {

public static void main(String[] args) {
// Old way -- vect will accept Collection
Vector vect = new Vector();
vect.add("Hello");
vect.add(12345);
vect.add(12.123);
System.out.println("Old method of using Collection");
for (Enumeration enu = vect.elements(); enu.hasMoreElements(); ) {
System.out.println( enu.nextElement() );
}

//We are forcing vetString to accept ONLY one type

Vector &ltString> vetString = new Vector &ltString>();
//Now vetString will only accept STRING values

vetString.add("Hello");
vetString.add("Everybody");
vetString.add("How are you?");

for (Enumeration enu = vetString.elements(); enu.hasMoreElements(); ) {
System.out.println( enu.nextElement() );
}
}

}

Example 2:-
package testnewfeatures;

import java.util.Vector;
import java.util.Enumeration;

public class UsingOwnGenericeClass {

public static void main(String[] args) {
GenericOrientedClass &ltinteger> intObj = new GenericOrientedClass &ltinteger>(100);
System.out.println( intObj.getValue() );

GenericOrientedClass &ltString> strObj = new GenericOrientedClass &ltString>("Hello World");
System.out.println( strObj.getValue() );

GenericOrientedClass &ltObject> objObj = new GenericOrientedClass &ltObject>("Hi this is Object Class string");
System.out.println( objObj.getValue() );

//Generics only Works with OBJECTS NOT with Primitives
//GenericOrientedClass &ltint> objObj = new GenericOrientedClass &ltint>(10);

}

}
class GenericOrientedClass&lttempname> {
private TempName str;

public GenericOrientedClass(TempName defaultValue){
str = defaultValue;
}

public void setValue(TempName s){
str = s;
}
public TempName getValue(){
return str;
}

}


New Java 5 Features - Part 1

Java Ver 1.5 or simply Java 5 came with some new features. These new features were added to facilitate a programmer not for the enhancement in JVM performance anymore.

Below i will describe few of them with some pretty examples. Hope you will find them helpful.

Autoboxing : - Just think automatically wrapping primitives into corresponding objects (i.e Integer,Boolean etc)

Check the example given below.

package testnewfeatures;

public class TestAutoBoxing {

public static void main(String[] args) {
int a = 10;
// old method of wrapping Primitive to Corresponding type Obj
Integer intObj1 = new Integer(a);
//new method of AutoBoxing - just created for programmer's ease NOT for a better performance
Integer intObj2 = a;

boolean bol = true;

System.out.println("\t Value of intObj1 = " + intObj1 );

System.out.println("\t Value of intObj2 = " + intObj2 );

if (bol) {
System.out.println("\t This is the Old way of Conditional-Cheack = " + bol );
}

Boolean bolObj = new Boolean(true);

// If requires ONLY Boolean Primitive but here we have given a Boolean Object
// Actually Java 5 automatically 'unbox' this for you.
if (bolObj) {
System.out.println("\t This is the NEW way of Conditional-Cheack = " + bolObj );
}
}

}



Scanners

Scanner are introduced to make new java programmers happy when they like to get some inputs from the system. Before this you were supposed to create a stream object and wrap it around another stream object & then you will have a chance to get a single input!.

Check the example given below.

package testnewfeatures;

import java.util.Scanner;

public class TestScanner {

public static void main(String[] args) {
String name = null;
int age = 0;
java.util.Scanner keyboard = new java.util.Scanner(System.in);


System.out.println("Please enter your Name ");
name = keyboard.nextLine();

System.out.println("Now enter you Age");
age = keyboard.nextInt();

System.out.println("Your Name is = " + name + "\nYour Age is = " + age);

System.out.println("Now reading a File from a drive \n" );
try {

String fileText = "";

java.util.Scanner readFile = new java.util.Scanner(new java.io.File("D:\\NetBeansProjetcs\\TestNewFeatures\\src\\testnewfeatures\\InputFile.txt"));

while (readFile.hasNextLine()) {
fileText += readFile.nextLine() + "\n";
}

System.out.println (fileText);
}
catch (Exception ex) {
ex.printStackTrace();
}

}

}


Static Import

If you want to use some properties or fields more then once then use Static Import one time and use only the created shortcuts on the remaining places.

Check the example given below.

package testnewfeatures;

import static java.lang.System.out;

public class TestStaticImport {

public static void main(String[] args) {


System.out.println("This is normal way to print something" );

out.println("This is the new way to print something" );
out.println("Here we have only 'Statically imported System.out' ");
out.println("so from now on we need no more ' System.out ' association.");

}

}



Monday 21 January 2008

Personal Training Material

I cam across a website where any one can find different articles regarding Personal Trainings to effectively improve professional life.

Here on the following link you will get it.
http://www.4shared.com/dir/4427129/defc974e/Training.html

Friday 18 January 2008

IELTS preparation material

Last days I was preparing my self for Ielts exam & came across few web sites which are providing good IELTS,Toefl, GRE preparation materials. If anyone interested to take the exam can easily find these websites helpful.

Ielts
http://www.esnips.com/_t_/ielts?to=
http://www.britishcouncil.org/pakistan-exams-ielts-prepare.htm


Toefl & GRE
http://www.universityportal.net/2007/07/toefl-gre-practice-tests.html
http://www.esnips.com/_t_/GRE?q=GRE
http://www.esnips.com/_t_/toefl?to=

Also search on Google using words like " Ielts Audio or mp3", "Ielts or Toelf, GRe Podcasts" . different words will bring out different search results.

Tuesday 15 January 2008

Joomla - new era of web designing

Joomla is a CMS (Content Management System) based web design toolkit/package/software. Nowadays CMS based web design getting attention worldwide. A web designer who is amateur or professional in his/her profession grab themas quickly as they understand it.

I was always involved in web design using old conventional system scripting, coding, creating templates, choosing color schemes and son on.... But the real power of CMS i discovered when i first created my local web site using it.

Amazingly i was surprised to see it's power of creating a full featured and standard based web sites easily & efficiently. This also supports Web 2.0 design features.

The way CMS - specially Joomla works is that it comes with different modules. Each module provides different functionalities.