JAVA generic, java generic

Source: Internet
Author: User

JAVA generic, java generic

Suppose we have the following requirement: how can we sort integer arrays, string arrays, and any other types of arrays by writing a sorting method?

Java generics can be used. With the concept of Java generics, we can write a generic method to sort an object array. Then, you can call this generic method to sort integer arrays, floating-point arrays, and string arrays.

 

Rules for defining generic methods:

  • All generic method declarations have a type parameter declaration section (separated by Angle brackets). This type parameter declaration section is prior to the type returned by the method (<E> In the following example ).
  • Each type parameter declaration part contains one or more type parameters separated by commas. A generic parameter, also known as a type variable, is an identifier used to specify a generic type name.
  • The type parameter can be used to declare the type of the returned value and can be used as a placeholder for the actual parameter type obtained by the generic method.
  • The Declaration of the generic method body is the same as that of other methods. Note that the type parameter can only represent the reference type, not the original type (such as int, double, char, etc ).
Public class GenericTest {public static <E> void printArray (E [] inputArray) {for (E element: inputArray) {System. out. print (element + "");} System. out. println ("");} public static void main (String args []) {Integer [] intArray = {1, 2, 3, 4, 5}; Double [] doubleArray = {1.1, 2.2, 3.3, 4.4, 5.5}; Character [] charArray = {'h', 'E', 'l', 'l', 'O '}; // It cannot be written as int, double, char printArray (intArray), printArray (doubleArray), printArray (charArray );}}

 

 

Bounded type parameters:

Sometimes, you may want to limit the types that are allowed to be passed to a type parameter. For example, a method to operate a Number may only accept instances of the Number or Number subclass. This is the purpose of bounded type parameters.

To declare a bounded type parameter, first list the name of the type parameter, followed by the extends keyword, and finally keep up with its upper bound.

Public class MaximumTest {// compare the three values and return the maximum value public static <N extends Comparable <N> N maximum (N x, N y, N z) {N max = x; // assume that x is the initial maximum value if (y. compareTo (max)> 0) {max = y; // y larger} if (z. compareTo (max)> 0) {max = z; // now z is bigger} return max; // return max object} public static void main (String args []) {System. out. printf ("% d, % d and % d the maximum number is % d \ n", 3, 4, 5, maximum (3, 4, 5 )); system. out. printf ("%. 1f, %. 1f and %. the maximum number in 1f is %. 1f \ n ", 6.6, 8.8, 7.7, maximum (6.6, 8.8, 7.7); System. out. printf ("% s, % s and % s, the maximum number is % s \ n", "pear", "apple", "orange", maximum ("pear ", "apple", "orange "));}}

 

Generic Type:

The declaration of generic classes is similar to that of non-generic classes, except that the declaration of type parameters is added after the class name.

Like the generic method, the type parameter declaration part of a generic class also contains one or more type parameters separated by commas. A generic parameter, also known as a type variable, is an identifier used to specify a generic type name. Because they accept one or more parameters, these classes are called parameterized classes or parameterized types.

 

Public class Box <T> {private T t; public void add (T t) {this. t = t;} public T get () {return t;} public static void main (String [] args) {Box <Integer> integerBox = new Box <Integer> (); box <String> stringBox = new Box <String> (); integerBox. add (new Integer (10); stringBox. add (new String ("cainiao tutorial"); System. out. printf ("INTEGER: % d \ n", integerBox. get (); System. out. printf ("string: % s \ n", stringBox. get ());}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.