Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Java syntax
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Generics== {{Main|Generics in Java}} [[Generic programming|Generics]], or parameterized types, or [[parametric polymorphism]], is one of the major features introduced in [[J2SE 5.0]]. Before generics were introduced, it was required to declare all the types explicitly. With generics, it became possible to work in a similar manner with different types without declaring the exact types. The main purpose of generics is to ensure type safety and to detect runtime errors during compilation. Unlike C#, information on the used parameters is not available at runtime due to [[type erasure]].<ref>{{Cite web |url=https://msdn.microsoft.com/en-us/library/f4a6ta2h.aspx |title=Generics in the Run Time (C# Programming Guide) |access-date=March 9, 2016 |archive-date=March 10, 2016 |archive-url=https://web.archive.org/web/20160310012449/https://msdn.microsoft.com/en-us/library/f4a6ta2h.aspx |url-status=live }}</ref> ===Generic classes=== {{Main|Generics in Java#Generic class definitions}} Classes can be parameterized by adding a type variable inside angle brackets (<code><</code> and <code>></code>) following the class name. It makes possible the use of this type variable in class members instead of actual types. There can be more than one type variable, in which case they are declared in a comma-separated list. It is possible to limit a type variable to a subtype of some specific class or declare an interface that must be implemented by the type. In this case the type variable is appended by the <code>extends</code> keyword followed by a name of the class or the interface. If the variable is constrained by both class and interface or if there are several interfaces, the class name is written first, followed by interface names with <code>& </code> sign used as the delimiter. <syntaxhighlight lang="java"> /* This class has two type variables, T and V. T must be a subtype of ArrayList and implement Formattable interface */ public class Mapper<T extends ArrayList & Formattable, V> { public void add(T array, V item) { // array has add method because it is an ArrayList subclass array.add(item); } } </syntaxhighlight> When a variable of a parameterized type is declared or an instance is created, its type is written exactly in the same format as in the class header, except the actual type is written in the place of the type variable declaration. <syntaxhighlight lang="java"> /* Mapper is created with CustomList as T and Integer as V. CustomList must be a subclass of ArrayList and implement Formattable */ Mapper<CustomList, Integer> mapper = new Mapper<CustomList, Integer>(); </syntaxhighlight> Since Java SE 7, it is possible to use a diamond (<code><></code>) in place of type arguments, in which case the latter will be inferred. The following code in Java SE 7 is equivalent to the code in the previous example: <syntaxhighlight lang="java"> Mapper<CustomList, Integer> mapper = new Mapper<>(); </syntaxhighlight> When declaring a variable for a parameterized type, it is possible to use wildcards instead of explicit type names. Wildcards are expressed by writing <code>?</code> sign instead of the actual type. It is possible to limit possible types to the subclasses or superclasses of some specific class by writing the <code>extends</code> keyword or the <code>super</code> keyword correspondingly followed by the class name. <syntaxhighlight lang="java"> /* Any Mapper instance with CustomList as the first parameter may be used regardless of the second one.*/ Mapper<CustomList, ?> mapper; mapper = new Mapper<CustomList, Boolean>(); mapper = new Mapper<CustomList, Integer>(); /* Will not accept types that use anything but a subclass of Number as the second parameter */ void addMapper(Mapper<?, ? extends Number> mapper) { } </syntaxhighlight> ===Generic methods and constructors=== Usage of generics may be limited to some particular methods, this concept applies to constructors as well. To declare a parameterized method, type variables are written before the return type of the method in the same format as for the generic classes. In the case of constructor, type variables are declared before the constructor name. <syntaxhighlight lang="java"> class Mapper { // The class itself is not generic, the constructor is <T, V> Mapper(T array, V item) { } } /* This method will accept only arrays of the same type as the searched item type or its subtype*/ static <T, V extends T> boolean contains(T item, V[] arr) { for (T currentItem : arr) { if (item.equals(currentItem)) { return true; } } return false; } </syntaxhighlight> ===Generic interfaces=== Interfaces can be parameterized in the similar manner as the classes. <syntaxhighlight lang="java"> interface Expandable<T extends Number> { void addItem(T item); } // This class is parameterized class Array<T extends Number> implements Expandable<T> { void addItem(T item) { } } // And this is not and uses an explicit type instead class IntegerArray implements Expandable<Integer> { void addItem(Integer item) { } } </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)