CMSC 132A Lecture Notes - Lecture 4: Type Inference

33 views4 pages

Document Summary

Generic methods are methods that introduce their own type parameters. This is similar to declaring a generic type, but the type parameter"s scope is limited to the method where it is declared. Static and non-static generic methods are allowed, as well as generic class constructors. The syntax for a generic method includes a list of type parameters, inside angle brackets, which appears before the method"s return type. For static generic methods, the type parameter section must appear before the method"s return type. The util class includes a generic method, compare, which compares two pair objects: public class util { The complete syntax for invoking this method would be: Pair p2 = new pair<>(2, pear); boolean same = util. compare(p1, p2); The type has been explicitly provided, as shown in bold. Generally, this can be left out and the compiler will infer the type that is needed:

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents

Related Questions