<< Chapter < Page | Chapter >> Page > |
Complete listings of the programs discussed in this module are provided below.
Listing 5 - The program named Generics01.
/*File Generics01.java
Copyright 2005, R.G.BaldwinIllustrates requirement to cast without the use
of generics.V1.5 compiler produces following warning:
Note: Generics01.java uses unchecked or unsafeoperations.
Note: Recompile with -Xlint:unchecked fordetails.
Recompilation with -Xlint:unchecked producesthe following output:
Generics01.java:34: warning: [unchecked]unchecked
call to add(E) as a member ofthe raw type ArrayList
var1.add(new Date());^
where E is a type-variable:E extends Object declared in class ArrayList
1 warningTested using JDK 1.7 under Win 7.
************************************************/import java.util.*;
public class Generics01{ArrayList var1 = new ArrayList();void runIt(){
var1.add(new Date());//Note the required cast in the following
// statement.System.out.println(
((Date)var1.get(0)).getTime());}//runIt
public static void main(String[]args){
new Generics01().runIt();}//end main}//end class Generics01
//=============================================//
Listing 6 - The program named Generics02.
/*File Generics02.java
Copyright 2005, R.G.BaldwinIllustrates the application of an incorrect cast
to an element that is fetched from an ArrayListobject and the runtime error produced by that
incorrect cast.This program does not produce a compiler error,
although the v1.5 compiler does produce ageneral warning having to do with the failure
to apply the new generics syntax released inv1.5. (Earlier compilers would not have produced
such a warning.)However, the compiler does not check to confirm
that the correct cast is applied. This resultsin the following runtime error when an incorrect
cast is applied:Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot
be cast to java.util.Dateat Generics02.runIt(Generics02.java:46)
at Generics02.main(Generics02.java:53)Tested using JDK 1.7 under Win 7.
************************************************/import java.util.*;
public class Generics02{ArrayList var1 = new ArrayList();void runIt(){var1.add(new Date());
var1.add("abcd");//Note that the (Date) cast is applied to
// an element of type String in the following// statement, producing a runtime error. The
// problem is that the wrong index was used// in fetching the element. Thus, the wrong
// element was fetched.System.out.println(
((Date)var1.get(1)).getTime());System.out.println(
((String)var1.get(1)).length());}//end runIt
public static void main(String[]args){
new Generics02().runIt();}//end main}//end class Generics02
//=============================================//
Listing 7 - The program named Generics03. |
---|
/*File Generics03.java
Copyright 2005, R.G.BaldwinIllustrates use of generics to avoid requirement
to cast. Requires v1.5 or later.Program output for one run was:
1377995720768Tested using JDK 1.7 under Win 7.
************************************************/import java.util.*;
public class Generics03{ArrayList<Date>var1 = new ArrayList<Date>();
void runIt(){var1.add(new Date());
//Note that no cast is required in the// following statement.
System.out.println(var1.get(0).getTime());}//end runIt
public static void main(String[]args){
new Generics03().runIt();}//end main}//end class Generics03
//=============================================// |
Listing 8 - The program named Generics04. |
---|
/*File Generics04.java
Copyright 2005, R.G.BaldwinIllustrates ability of generics to prevent
storing of wrong type in a collection.Requires v1.5 or later.
Compilation produces following error message:Generics04.java:34: error: no suitable method
found for add(String)var1.add("abcd");
^method ArrayList.add(int,Date) is not
applicable (actual and formal argument listsdiffer in length)
method ArrayList.add(Date) is not applicable(actual argument String cannot be converted
to Date by method invocation conversion)1 error
Once the ArrayList has been declared to be oftype Date, it is not possible to add an element
of type String. An attempt to do so producesa compiler error.
Tested using JDK 1.7 under Win 7.************************************************/
import java.util.*;public class Generics04{ArrayList<Date>var1 = new ArrayList<Date>();void runIt(){
var1.add("abcd");System.out.println(var1.get(0).getTime());
}//end runItpublic static void main(String[] args){new Generics04().runIt();
}//end main}//end class Generics04
//=============================================// |
Listing 9 - The program named Generics05.
/*File Generics05.java
Copyright 2005, R.G.BaldwinIllustrates required syntax for using an iterator
with generics. Also illustrates the new for-eachconstruct in Java 5.0
Output for one particular run is shown below.1378070280877
13781566808771378243080877
13780702808771378156680877
1378243080877Output will be different each time the program
is run depending on the current date and time.Tested using JDK 1.7 under Win 7.
************************************************/import java.util.*;
public class Generics05{//Create an ArrayList object suitable for
// storing references to Date objects.ArrayList<Date>var1 = new ArrayList<Date>();void runIt(){
//Get current date and time in milliseconds.long now = new Date().getTime();
//Get length of one day in millisecondslong oneDay = 24 * 60 * 60 * 1000;
//Populate the ArrayList objectvar1.add(new Date(now));
var1.add(new Date(now + oneDay));var1.add(new Date(now + 2 * oneDay));
//Get an iteratorIterator<Date>iter = var1.iterator();//Perform the iteration
while(iter.hasNext()){System.out.println(iter.next().getTime());
}//end while loopSystem.out.println();//blank line//Now perform the same iteration using// the new for-each construct.
for(Date element : var1){System.out.println(element.getTime());
}//end for-each}//end runIt
public static void main(String[]args){
new Generics05().runIt();}//end main
}//end class Generics05//=============================================//
-end-
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?