Save
:(OOP
Save
Share
Learn
Content
Leaderboard
Learn
Created by
Hana Mae
Visit profile
Subdecks (2)
DATA1
:(OOP
5 cards
01
:(OOP
14 cards
Cards (30)
Generics
Parameterized types
that allow type (Integer, String, etc. and user-defined types) to be a parameter to methods, classes, and interfaces
Generic entity
Class, interface, or method that
operates
on
a
parameterized
type
Object
is the
superclass
of
all
other
classes
Introduced Java Generics
JDK
5.0
Purpose of Java Generics
Reducing
bugs
and
adding
an
extra
layer
of
abstraction
over types
When to use Generics
When you want to create
classes
or methods that can operate with any data type while maintaining
type safety
When you want to avoid
code duplication
by writing reusable components that work with different
data types
When working with
collections
to
ensure
type
safety
and prevent
runtime errors
When
designing
APIs
or
libraries
that need to be
flexible
and adaptable to different data types
Code Reuse
Write a method/class/interface once and use it for any type we want, write code that will work with
different types
of
data
Type Safety
Generics
make
errors appear
at compile time rather than at run time
Type Parameters
<
T
>
Type parameters
, also known as type variables, are used as placeholders to indicate that a
type
will be assigned to the class at runtime
Common Type Parameter
Names
E
-
Element
K
-
Key
N -
Number
T -
Type
V
-
Value
S, U, V, and so on: Second,
third
, and
fourth
types in a multiparameter situation
You
cannot
use
Primitive
Data
Types
like int, double, float, char etc. You must use a
wrapper
class.
See all 30 cards