Contents - Cultural View
Contents - Cultural View Contents - Cultural View
FormEngine 124 See also • JavaServer Faces • XForms • Google Web Toolkit External links • form-engine.de Home [2] • FormEngine example page [4] • Chiba Engine Home [5] • imatics ProcessSuite [6] - A business process management suite that makes use of the FormEngine framework References [1] http:/ / www. imatics. de [2] http:/ / www. form-engine. de/ en/ index. jsp [3] Java Magazine (2010), " it-republik.de (http:/ / it-republik. de/ jaxenter/ artikel/ FormEngine-Multidynamische-Formulare-2893. html)". FormEngine: Multidynamische Formulare [4] http:/ / www. form-engine. de/ en/ examples/ examples. jsp [5] http:/ / chiba-project. org/ chiba/ [6] http:/ / www. processsuite. de/ langen/ index. html Generics in Java Generics are a facility of generic programming that was added to the Java programming language in 2004 as part of J2SE 5.0. They allow "a type or method to operate on objects of various types while providing compile-time type safety." [1] Hierarchy and classification As per Java Language Specification [2] : • A type variable is an unqualified identifier. Type variables are introduced by generic class declarations, generic interface declarations, generic method declarations, and by generic constructor declarations. • A class is generic if it declares one or more type variables. These type variables are known as the type parameters of the class. It defines one or more type variables that act as parameters. A generic class declaration defines a set of parameterized types, one for each possible invocation of the type parameter section. All of these parameterized types share the same class at runtime. • An interface is generic if it declares one or more type variables. These type variables are known as the type parameters of the interface. It defines one or more type variables that act as parameters. A generic interface declaration defines a set of types, one for each possible invocation of the type parameter section. All parameterized types share the same interface at runtime. • A method is generic if it declares one or more type variables. These type variables are known as the formal type parameters of the method. The form of the formal type parameter list is identical to a type parameter list of a class or interface. • A constructor can be declared as generic, independently of whether the class the constructor is declared in is itself generic. A constructor is generic if it declares one or more type variables. These type variables are known as the formal type parameters of the constructor. The form of the formal type parameter list is identical to a type parameter list of a generic class or interface.
Generics in Java 125 Motivation for generics The following block of Java code illustrates a problem that exists when not using generics. First, it declares an ArrayList of type Object. Then, it adds a String to the ArrayList. Finally, it attempts to retrieve the added String and cast it to an Integer. List v = new ArrayList(); v.add("test"); Integer i = (Integer)v.get(0); // Run time error Although the code compiles without error, it throws a runtime exception (java.lang.ClassCastException) when executing the third line of code. This type of problem can be avoided by using generics and is the primary motivation for using generics. Using generics, the above code fragment can be rewritten as follows: List v = new ArrayList(); v.add("test"); Integer i = v.get(0); // (type error) Compile time error The type parameter String within the angle brackets declares the ArrayList to be constituted of String (a descendant of the ArrayList's generic Object constituents). With generics, it is no longer necessary to cast the third line to any particular type, because the result of v.get(0) is defined as String by the code generated by the compiler. Compiling the third line of this fragment with J2SE 5.0 (or later) will yield a compile-time error because the compiler will detect that v.get(0) returns String instead of Integer. For a more elaborate example, see [3] . Here is a small excerpt from the definitions of the interfaces List and Iterator in package java.util: public interface List { } void add(E x); Iterator iterator(); public interface Iterator { } E next(); boolean hasNext(); Wildcards Generic type parameters in Java are not limited to specific classes. Java allows the use of wildcards to specify bounds on the type of parameters a given generic object may have. Wildcards are type parameters of the form "?", possibly annotated with a bound. Given that the exact element type of an object with a wildcard is unknown, restrictions are placed on the type of methods that may be called on the object. As an example of an unbounded wildcard, List indicates a list which has an unknown object type. Methods which take such a list as an argument can take any type of list, regardless of parameter type. Reading from the list will return objects of type Object, and writing non-null elements to the list is not allowed, since the parameter type is not known. To specify the upper bound of a generic element, the extends keyword is used, which indicates that the generic type is a subtype of the bounding class. Thus it must either extend the class, or implement the interface of the bounding class. So List
- Page 77 and 78: Comparison of Java and C Sharp 73 t
- Page 79 and 80: Comparison of Java and C Sharp 75 N
- Page 81 and 82: Comparison of Java and C Sharp 77 L
- Page 83 and 84: Comparison of Java and C Sharp 79 T
- Page 85 and 86: Comparison of Java and C Sharp 81 C
- Page 87 and 88: Comparison of Java and C++ 83 C++ J
- Page 89 and 90: Comparison of Java and C++ 85 e="7.
- Page 91 and 92: Comparison of Java and C++ 87 the c
- Page 93 and 94: Comparison of Java and C++ 89 • D
- Page 95 and 96: Comparison of Java and C++ 91 Sever
- Page 97 and 98: Comparison of the Java and .NET pla
- Page 99 and 100: Comparison of the Java and .NET pla
- Page 101 and 102: Comparison of the Java and .NET pla
- Page 103 and 104: Comparison of the Java and .NET pla
- Page 105 and 106: Constant interface 101 } public sta
- Page 107 and 108: Java 4K Game Programming Contest 10
- Page 109 and 110: Java 4K Game Programming Contest 10
- Page 111 and 112: CookXml 107 Creators, setters, adde
- Page 113 and 114: Cougaar 109 Cougaar Cougaar is a Ja
- Page 115 and 116: Devoxx 111 Related links • Devoxx
- Page 117 and 118: Elastic Path 113 References [1] htt
- Page 119 and 120: Electronic Disturbance Theater 115
- Page 121 and 122: Event dispatching thread 117 • Sw
- Page 123 and 124: Facelets 119 The above code can be
- Page 125 and 126: final (Java) 121 } Sphere(double x,
- Page 127: FormEngine 123 FormEngine Developer
- Page 131 and 132: Generics in Java 127 Pair grade440
- Page 133 and 134: G-java 129 G-java G-java 2.x was a
- Page 135 and 136: GlassFish Metro 131 Features Metro
- Page 137 and 138: James Gosling 133 Contributions He
- Page 139 and 140: Todd Greanier 135 Todd Greanier Tod
- Page 141 and 142: Head First (book series) 137 Head F
- Page 143 and 144: Inner class 139 • Local class - T
- Page 145 and 146: Interface (Java) 141 The body of th
- Page 147 and 148: Internet Foundation Classes 143 Int
- Page 149 and 150: Internet Foundation Classes 145 •
- Page 151 and 152: JAD (file format) 147 JAD (file for
- Page 153 and 154: JAMWiki 149 JAMWiki Developer(s) Ry
- Page 155 and 156: JGroups 151 JGroups Stable release
- Page 157 and 158: Java Class Library 153 Java Class L
- Page 159 and 160: Java Class Library 155 Alternative
- Page 161 and 162: Java Classloader 157 User-defined c
- Page 163 and 164: Java compiler 159 Java compiler A J
- Page 165 and 166: Java Development Kit 161 • jstack
- Page 167 and 168: Java Interface Definition Language
- Page 169 and 170: Java TV 165 See also • Xlet Exter
- Page 171 and 172: Java Work Framework 167 Database db
- Page 173 and 174: Java annotation 169 programmaticall
- Page 175 and 176: Java annotation 171 component.getAt
- Page 177 and 178: Java collections framework 173 Refe
Generics in Java 125<br />
Motivation for generics<br />
The following block of Java code illustrates a problem that exists when not using generics. First, it declares an<br />
ArrayList of type Object. Then, it adds a String to the ArrayList. Finally, it attempts to retrieve the added String and<br />
cast it to an Integer.<br />
List v = new ArrayList();<br />
v.add("test");<br />
Integer i = (Integer)v.get(0); // Run time error<br />
Although the code compiles without error, it throws a runtime exception (java.lang.ClassCastException) when<br />
executing the third line of code. This type of problem can be avoided by using generics and is the primary motivation<br />
for using generics.<br />
Using generics, the above code fragment can be rewritten as follows:<br />
List v = new ArrayList();<br />
v.add("test");<br />
Integer i = v.get(0); // (type error) Compile time error<br />
The type parameter String within the angle brackets declares the ArrayList to be constituted of String (a descendant<br />
of the ArrayList's generic Object constituents). With generics, it is no longer necessary to cast the third line to any<br />
particular type, because the result of v.get(0) is defined as String by the code generated by the compiler.<br />
Compiling the third line of this fragment with J2SE 5.0 (or later) will yield a compile-time error because the<br />
compiler will detect that v.get(0) returns String instead of Integer. For a more elaborate example, see [3] .<br />
Here is a small excerpt from the definitions of the interfaces List and Iterator in package java.util:<br />
public interface List {<br />
}<br />
void add(E x);<br />
Iterator iterator();<br />
public interface Iterator {<br />
}<br />
E next();<br />
boolean hasNext();<br />
Wildcards<br />
Generic type parameters in Java are not limited to specific classes. Java allows the use of wildcards to specify<br />
bounds on the type of parameters a given generic object may have. Wildcards are type parameters of the form "?",<br />
possibly annotated with a bound. Given that the exact element type of an object with a wildcard is unknown,<br />
restrictions are placed on the type of methods that may be called on the object.<br />
As an example of an unbounded wildcard, List indicates a list which has an unknown object type. Methods<br />
which take such a list as an argument can take any type of list, regardless of parameter type. Reading from the list<br />
will return objects of type Object, and writing non-null elements to the list is not allowed, since the parameter type is<br />
not known.<br />
To specify the upper bound of a generic element, the extends keyword is used, which indicates that the generic type<br />
is a subtype of the bounding class. Thus it must either extend the class, or implement the interface of the bounding<br />
class. So List