ADBRITE ads links
You are here: CodeIdol.com > Java > The Java Tutorial Fourth Edition: A Short Course on the Basics > Generics
The Java Tutorial Fourth Edition: A Short Course on the Basics
| Chapter 6. Generics
IntroductionGeneric TypesGeneric Methods and ConstructorsBounded Type ParametersSubtypingWildcardsType ErasureSummary of GenericsQuestions and Exercises: Generics
...
|
|
| Introduction
In any nontrivial software project, bugs are simply a fact of life. Careful planning, programming, and testing can help reduce their pervasiveness, but somehow, somewhere, they'll always find a way to creep into your code. This become...
|
|
| Generic Types
Let's update our Box class to use generics. We'll first create a generic type declaration by changing the code "public class Box" to "public class Box<T>"; this introduces one type variable, named T, that ca...
|
|
| Generic Methods and Constructors
Type parameters can also be declared within method and constructor signatures to create generic methods and generic constructors. This is similar to declaring a generic type, but the type parameter's scope is limit...
|
|
| Bounded Type Parameters
There may be times when you'll want to restrict the kinds 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 subcl...
|
|
| Subtyping
As you already know, it's possible to assign an object of one type to an object of another type provided that the types are compatible. For example, you can assign an Integer to an Object, since Object is one of Integer's supertypes:
...
|
|
| Wildcards
Earlier we mentioned that English is ambiguous. The phrase "animal cage" can reasonably mean "all-animal cage," but it also suggests an entirely different concept: a cage designed not for any kind of animal, but rathe...
|
|
| Type Erasure
When a generic type is instantiated, the compiler translates those types by a technique called type erasurea process where the compiler removes all information related to type parameters and type arguments within a class or method. Ty...
|
|
| Summary of Generics
This chapter described the following problem: We have a Box class, written to be generally useful so that it deals with Objects. We need an instance that takes only Integers. The comments say that only Integers go in, so the pr...
|
|
| Questions and Exercises: Generics
Questions
Consider the following classes:
public class AnimalHouse<E> {
private E animal;
public void setAnimal(E x) {
animal = x;
}
public E getAnimal() {
return animal;
}
}
...
|
|
You are here: CodeIdol.com > Java > The Java Tutorial Fourth Edition: A Short Course on the Basics > Generics
|
|
Related tags
Popular Categories
Unix books and guides
AJAX popular information
C# language guides
Windows books and cookbooks
.......
|
|