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 <String>();
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 <String> vetString = new Vector <String>();
//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 <integer> intObj = new GenericOrientedClass <integer>(100);
System.out.println( intObj.getValue() );
GenericOrientedClass <String> strObj = new GenericOrientedClass <String>("Hello World");
System.out.println( strObj.getValue() );
GenericOrientedClass <Object> objObj = new GenericOrientedClass <Object>("Hi this is Object Class string");
System.out.println( objObj.getValue() );
//Generics only Works with OBJECTS NOT with Primitives
//GenericOrientedClass <int> objObj = new GenericOrientedClass <int>(10);
}
}
class GenericOrientedClass<tempname> {
private TempName str;
public GenericOrientedClass(TempName defaultValue){
str = defaultValue;
}
public void setValue(TempName s){
str = s;
}
public TempName getValue(){
return str;
}
}