Programming Tutorials Browser Tutorials Articles Struts Tutorials Hibernate Tutorials

  Tutorial: Taming Tiger

Taming Tiger, Part 2 Understanding generics Welcome to the second part of this three-part series on Sun Microsystems' latest release of the Java 2 Platform, Standard Edition (J2SE). To refresh your memory, Part 1 was a quick introduction to J2SE 1.5

Tutorial Details:

Enter Java generics
Now, let's rewrite the above code fragment using generics:


// Class A and B declared previously

// Somewhere in the program create a Vector
Vector v = new Vector();

// Add an object of type A
v.add(new A());

// And sometime later get the object back
B b = (B) v.get(0);


That looks similar to the first fragment, except for the code in the angle brackets. The angle brackets are the syntax for providing type parameters to parameterized types. I talk more about both parameterized types and type parameters in this article's later sections.

Even though the code change is minimal, its impact is far from small. This code fragment will not compile. Compiling this program with J2SE 1.5 (remember to use -source 1.5) will give you the following error:


inconvertible types
found : A
required: B
B b = (B) v.get(0);


In plain English, the compiler tells us that to be able to use the line B b = (B) v.get(0); legally, the vector must be parameterized (in the declaration) to accept objects of type B.


 

Read Tutorial at: Click here to view the tutorial

Rate Tutorial:
Taming Tiger

View Tutorial:
Taming Tiger

Related Tutorials:

Language improvements and models make great Java - JavaWorld
Language improvements and models make great Java - JavaWorld
 
Facilitate form
Facilitate form processing with the Form Processing API
 
Java Tip 132: The taming of the thread
Java Tip 132: The taming of the thread
 
Declarative Programming in Java
Declarative Programming in Java What makes EJB components special is the declarative programming model through which we can specify the services such as security, persistence, transaction etc., that the container should provide. An EJB only implements
 
Taming Tiger
Taming Tiger, Part 2 Understanding generics Welcome to the second part of this three-part series on Sun Microsystems' latest release of the Java 2 Platform, Standard Edition (J2SE). To refresh your memory, Part 1 was a quick introduction to J2SE 1.5
 
Taming Tiger, Part 3
J2SE 5—code named "Tiger"—is the most significant revision to the Java language since its original inception. Tarak Modi's primary goal with his three-part series on Tiger is to familiarize readers with J2SE 5's most important additions and show how t
 
Creating Varargs in Java 1.5 Tiger
Creating Varargs in Java 1.5 Tiger In this excerpt from Chapter 5 of the book, Brett and David cover how to create and iterate over variable-length argument lists (better known as varargs), which will have you writing better, cleaner, more flexible code
 
Annotations in Tiger, Part 1: Add metadata to Java code
Annotations, a new feature in J2SE 5.0 (Tiger), brings a much-needed metadata facility to the core Java language. In this first of a two-part series, author Brett McLaughlin explains why metadata is so useful, introduces you to annotations in the Java lan
 
Annotations in Tiger, Part 2: Custom annotations
Write your own annotations in Java 5 Part 1 of this series introduced annotations, the new metadata facility in J2SE 5.0, and focused on Tiger's basic built-in annotations. A more powerful related feature is support for writing your own annotations. In t
 
Monitoring Local and Remote Applications Using JMX 1.2 and JConsole
Monitoring Local and Remote Applications Using JMX 1.2 and JConsole The latest release of Java, J2SE 5.0 (codenamed Tiger), adds core support for the Java Management Extensions (JMX) 1.2 into the Java standard libraries. This article walks you through h
 
Tiger and Beyond, the Future of the Java Platform
Part Two of an interview with Sun Microsystems' Sun Fellow, Graham Hamilton, explores Java 2 Platform, Standard Edition 5.0 (J2SE 5.0) and the future of the Java language.
 
Five Reasons to Move to the J2SE 5 Platform
Five important reasons to move to the Java 2 Platform, Standard Edition (J2SE platform) 5.0, supported by data and references to prove that the 5.0 release will reduce development and runtime costs.
 
JavaRSS.com 2004: Review of the Year
A look back at the major events of 2004 in Java.
 
Site navigation
 

 

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2006. All rights reserved.