- Generic methods are defined using type parameters.
- Every generic method has a type parameters section that
precedes the return type of the method
- The type parameters are separated by commas and enclosed
in angle brackets < and >
- Type parameters also known as type variables are identifiers
that specify generic type names
- In generic method type parameters are used for:
- Declare the return type of method
- Declare formal parameters
- Declare local variables
Generic Classes:
Just as Generic methos you can define Generic classes. In
this case, the type parameters are enclosed in angle brackets <
and > and placed after name of the class and a type parameter
is used to declare variables and generic methods with in the
class
public class GenericsSnippet {
private E obj;
//Generic constructor
GenericsSnippet(){}
//Getting the Generic Value
public E getVal(){
return obj;
}
//Setting the Generic object value.
public void setVal(E obj){
this.obj = obj;
}
}
- Generic classes are used to write a single definition
for a set of related classes.
- Generic classes are also known as parametric classes
- In generic classes you can declare reference variables
using type parameter within the definition of the class
- Objects cannot be instantiated using type parameters
Bounded Type parameters
Some times you will need to restrict the kind of types that
are allowed to be passed to a type parameter For example a method
that operates on numbers might only want to accept instances
of Number or its subclasses (Integer, float etc.).
To declare a bounded type parameter, list the type parameter's
name, followed by the extends keyword, followed by its upper
bound.
public class FindMax
{
public static <T extends Comparable <T>> T maximum(T x, T y, T z)
{
T max = x;
if ( y.compareTo( max ) > 0 )
{
max = y;
}
if ( z.compareTo( max ) > 0 )
{
max = z;
}
return max;
}
public static void main( String args[] )
{
System.out.printf( "Max of %d, %d and %d is %d\n\n", 3, 4, 5, maximum( 3, 4, 5 ) );
System.out.printf( "Maximum of %.1f,%.1f and %.1f is %.1f\n\n", 6.6, 8.8, 7.7, maximum( 6.6, 8.8, 7.7 ) );
System.out.printf( "Max of %s, %s and %s is %s\n","pear","apple", "orange", maximum( "pear", "apple", "orange" ) );
}
}
Wildcard types
Wild card types are used for putting restrictions on the
types that are used in generic programming. These are used as
type parameters in the declarations of variables and of formal
parameters in method definitions.
Difference between Wildcard bound and type parameter bound
- A wildcard can have only one bound, while a type parameter
can have several bounds
- A wildcard can have a lower or an upper bound, while
there is no such thing as a lower bound for a type parameter.
7. Types of Java Applications
There are mainly 4 types of applications that can be created using Java programming:
1) Standalone Application: Standalone applications are also known as desktop applications or window-based applications. These are traditional software that we need to install on every machine. Examples of standalone application are Media player, antivirus, etc. AWT and Swing are used in Java for creating standalone applications.
2) Web Application
: An application that runs on the server side and creates a dynamic page is called a web application. Currently, Servlet, JSP, Struts, Spring, Hibernate, JSF, etc. technologies are used for creating web applications in Java.
3) Enterprise Application: An application that is distributed in nature, such as banking applications, etc. is called an enterprise application. It has advantages like high-level security, load balancing, and clustering. In Java, EJB is used for creating enterprise applications.
4) Mobile Application:
An application which is created for mobile devices is called a mobile application. Currently, Android and Java ME are used for creating mobile applications.