0% found this document useful (0 votes)
52 views

Lecture 4 - Introduction To Generics: There Are Two Types of Variables in Java: A Reference Variable

This document provides an introduction and overview of generics in Java. It recaps types of variables, wrapper classes, and autoboxing/unboxing. It then defines that a generic class uses a type parameter like <T> after the class name. When instantiated, the type parameter is replaced by the type argument. An example Generic class Sample<T> is provided to demonstrate how the type parameter T restricts the types that can be passed to and returned from its methods. Finally, it notes some key points about using generics, like only using <T> in the class header, not constructor headers, and that primitives require wrapper classes as type arguments.

Uploaded by

tototo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Lecture 4 - Introduction To Generics: There Are Two Types of Variables in Java: A Reference Variable

This document provides an introduction and overview of generics in Java. It recaps types of variables, wrapper classes, and autoboxing/unboxing. It then defines that a generic class uses a type parameter like <T> after the class name. When instantiated, the type parameter is replaced by the type argument. An example Generic class Sample<T> is provided to demonstrate how the type parameter T restricts the types that can be passed to and returned from its methods. Finally, it notes some key points about using generics, like only using <T> in the class header, not constructor headers, and that primitives require wrapper classes as type arguments.

Uploaded by

tototo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

4/3/18

Lecture 4 –
Introduction to
Generics

CS 240 – Data Structures & Algorithms I

Recap: Types of variables


• There are two types of variables in Java:

• A reference variable:
– Stores the memory location of an object.

• A primitive variable:
– Directly stores the value.
int x = 50;
– x is a primitive variable: it doesn’t store a memory location, but
rather the number 50.

1
4/3/18

Recap: Wrapper classes


• Certain types of methods can be designed to accept any
type of Java Object as an argument (which we will see
momentarily).

• However, these types of methods do not accept primitive


types.

• Java provides wrapper classes, which each wrap a


primitive variable in an Object. We can pass primitives to
methods that require Object arguments by placing them
into wrapper classes first.

Recap: Wrapper Classes


• There are 8 primitive types in Java, and they each have
their own wrapper class.
Primitive Wrapper
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean

2
4/3/18

Recap: Autoboxing/auto-unboxing

Java uses autoboxing to automatically wrap a primitive type


into a wrapper Object:
Integer i = new Integer(3); // boxing, add primitive value to wrapper object
i = 4; // autoboxing

It also uses auto-unboxing to retrieve a primitive value from a


wrapper Object:
int j = i.intValue(); // unboxing, get primitive value from wrapper object
j = i; // auto-unboxing

Generics
• A generic class is written by placing a generic type
parameter (for example, T) in angle brackets immediately
after the class name in the class header.

• The name can be any legal Java identifier, but by


convention we usually use single capital letters such as T
(abbreviation for "type") or E (abbreviation for "element")

• When we instantiate a Sample object, we pass a type


argument which replaces each type parameter with that
argument.

3
4/3/18

Generics - example

public class Sample<T>


{
private T data;

public void setData(T newData)


{
data = newData;
}

public T getData()
{
return data;
}
}

Generics – Type Argument


If we instantiate a Sample object with a String type argument:
Sample<String> mySample = new Sample<>();

public class Sample<T>


{
private T data;
public void setData(T newData) { data = newData; }
public T getData() { return data; }
}

The mySample Object will be instantiated as if its class were


written this way:
public class Sample
{
private String data;
public void setData(String newData) {data = newData; }
public String getData() { return data; }
}

4
4/3/18

Generics
Once we instantiate a generic object with a type argument,
any of its methods that accept a parameter of that generic
type only accept arguments of the same type as the type
argument:
Sample<String> mySample = new Sample<>();
mySample.setData("Some Data"); // allowed!
mySample.setData(5); // not allowed!
String data = mySample.getData(); // allowed
int data = mySample.getData(); // not allowed!

setData() only accepts Strings and getData() only returns Strings


once mySample has been instantiated with String as the type
argument.

Generics

Note: When we want to use a primitive type as a type


argument, we must use a wrapper class instead:

Sample<Integer> mySample = new Sample<>(); // allowed!


Sample<int> mySample = new Sample<>(); // not allowed!

However, once mySample is instantiated, we can pass


primitives to its methods as Java provides autoboxing:

mySample.setData(1); // allowed!
int value = mySample.getData(); // also allowed!

5
4/3/18

Generics - notes
Within the definition of a class name<T>, where T is a
generic type parameter,
• <T> follows the identifier name in the class’s header
• <T> does not follow the names of the constructors in their
definitions
• T—not <T>—can be a data type of data fields, method
parameters, and local variables, and it can be a return type of
methods

Example – Pairable Interface

6
4/3/18

Example – OrderedPair class

Example – OrderedPair class


● See OrderedPairDriver.java

You might also like