javapandit.net
  • Home
  • Quick Java
    • Exception Handling
    • Collections Framework
  • Java Best Practices
  • Web Services
    • Web Service Basics
    • Ten Basic webservice concepts
    • XML
    • Apache Axis
    • Restful Web Services
  • JMS Concepts
    • JMS- MySQL
  • Hadoop
    • NoSQL DATABASEs
    • Apache Sqoop
    • Hadoop Interview Questions
  • Java 5
  • Java 8
    • Java 8 : Lambda Expressions
  • JDBC
  • Java Architect
    • Enterprise application re-platforming strategies
    • Java Memory Management
  • Java Programs
  • Technical Tips
    • How to set JAVA_HOME environment variable
    • How to create an auto increment field in Oracle?
    • Linux Commands
  • Best Java Interview Questions
    • Java Interview Questions- YouTube
  • Interview Questions
    • Java Tech interview Questions
    • Core Java Interview Questions >
      • core tech questions1
      • Java Collection interview questions
      • Java Concurrency
    • Servlets Interview Questions
    • JSP Interview Questions
    • Java Web Services Interview Questions
    • EJB Interview Questions
    • XML Interview Questions
    • JMS Interview Questions
  • Struts Interview Questions
    • Struts 2 Interview Questions
  • Java EE Architects Interview Questions
    • Java Architect Interview Questions
    • Top 10 reasons for Java Enterprise Application Performance Problems
    • Web Application Scalability Questions for IT Architect
  • JavaPandit's Blog
  • Web Services Interview Questions
  • Servlets And JSP
  • Oracle SOA Interview Questions
    • Open ESB /JBI
    • BPEL Language
  • Log4J
  • Ant
  • Maven
  • JMeter
  • JUnit
  • Apache POI Framework
  • ORCALE SERVICE BUS (OSB) Interview Questions
  • J2EE Patterns
    • Model-View-Controller (MVC)
    • Front Controller
    • DAO
    • Business Delegate
    • Session Facade
    • Service Locator
    • Transfer Object
    • Design Patterns >
      • Creational Patterns >
        • Singleton
      • Behavioural Patterns
      • Structural Patterns
    • Intercepting Filter
  • SQL Interview Questions/Lab
  • Best Wall Papers
    • Devotional Songs
  • Java Community
  • HIBERNATE
  • ORACLE CC&B
    • Oracle CC&B Interview Questions
  • Docker
  • Little Princess
    • Sai Tanvi Naming Ceremony Celebrations
    • Rice Feeding Ceremony
    • Sai Tanvi Gallery
  • APPSC Career Guidance
    • AP History
    • Indian Polity
    • Indian Economy
    • Science & Technology
    • Mental Ability and Reasoning
    • Disaster Management
    • Current Affairs and Events
    • General Sciences >
      • Biology
      • Physics
      • Chemistry
    • Previous Question Papers
  • About Us
  • Contact US
Arrays
Arrays of generic types and arrays of type variables are not allowed. Attempting to create an array of
parameterized Vectors, for example, causes a compiler error:

import java.util.*;
public class GenericArrayExample {
public static void main(String args[])
{
Vector<Integer> vectorList[] = new Vector<Integer>[10];
}
}

If you try to compile that code, the compiler issues the following two errors. This code is the simplest
approach to creating an array of a generic type and the compiler tells you explicitly that creating a
generic type array is forbidden:

GenericArrayExample.java:6: arrays of generic types are not allowed
Vector<Integer> vectorList[] = new Vector<Integer>[10];
                        ^
GenericArrayExample.java:6: arrays of generic types are not allowed
Vector<Integer> vectorList[] = new Vector<Integer>[10];
                                                    ^
2 errors
You can, however, create an array of any type by using the wildcard as the type parameter.

Generic Methods

In addition to the generic mechanism for classes, generic methods are introduced. The angle brackets for
the parameters appear after all method modifiers but before the return type of the method. Following is
an example of a declaration of a generic method:

static <Elem> void swap(Elem[] a, int i, int j)
{
Elem temp = a[i];
a[i] = a[j];
a[j] = temp;
}

The syntax for the parameters in a generic method is the same as that for generic classes. Type variables can
have bounds just like they do in class declarations. Two methods cannot have the same name and argument
types. If two methods have the same name and argument types, and have the same number of type
variables with the same bounds, then these methods are the same and the compiler will generate an error.

Generics and Exceptions
Type variables are not permitted in catch clauses, but can be used in throws lists of methods. An example
of using a type variable in the throws clause follows. The Executor interface is designed to execute
a section of code that may throw an exception specified as a parameter. In this example, the code that
fills in the execute method might throw an IOException. The specific exception, IOException, is
specified as a parameter when creating a concrete instance of the Executor interface:

import java.io.*;
interface Executor<E extends Exception> {
void execute() throws E;
}
public class GenericExceptionTest {
public static void main(String args[]) {
try {
Executor<IOException> e =
new Executor<IOException>() {
public void execute() throws IOException
{
// code here that may throw an
// IOException or a subtype of
// IOException
}
};
e.execute();
} catch(IOException ioe) {
System.out.println(“IOException: “ + ioe);
ioe.printStackTrace();
}
}
}

The specific type of exception is specified when an instance of the Executor class is created inside main.
The execute method throws an arbitrary exception that it is unaware of until a concrete instance of the
Executor interface is created.
Prev 1 2 Next
Powered by Create your own unique website with customizable templates.