Get 2 days of premium access
Study Guides (380,000)
US (220,000)
UMD (10,000)
CMSC (1,000)
Study Guide

CMSC 132A- Final Exam Guide - Comprehensive Notes for the exam ( 78 pages long!)Premium


Department
Computer Science
Course Code
CMSC 132A
Professor
David Van Horn
Study Guide
Final

This preview shows pages 1-3. to view the full 78 pages of the document.
UMD
CMSC 132A
Final EXAM
STUDY GUIDE

Only pages 1-3 are available for preview. Some parts have been intentionally blurred.

Subscribers Only

Only pages 1-3 are available for preview. Some parts have been intentionally blurred.

Subscribers Only
CMSC132A Lecture 1: Generics
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 becomes especially
apparent as new features are introduced and your code base grows in size and
complexity.
Fortunately, some bugs are easier to detect than others. Compile-time bugs, for
example, can be detected early on; you can use the compiler's error messages to figure
out what the problem is and fix it, right then and there. Runtime bugs, however, can be
much more problematic; they don't always surface immediately, and when they do, it
may be at a point in the program that is far removed from the actual cause of the
problem.
Why Use Generics?
In a nutshell, generics enable types
(classes and interfaces) to be parameters when
defining classes, interfaces and methods. Much like the more familiar formal parameters
used in method declarations, type parameters provide a way for you to re-use the same
code with different inputs. The difference is that the inputs to formal parameters are
values, while the inputs to type parameters are types.
Code that uses generics has many benefits over non-generic code:
Stronger type checks at compile time
A Java compiler applies strong type checking to generic code and issues errors if the
code violates type safety. Fixing compile-time errors is easier than fixing runtime errors,
which can be difficult to find.
find more resources at oneclass.com
find more resources at oneclass.com
You're Reading a Preview

Unlock to view full version

Subscribers Only

Only pages 1-3 are available for preview. Some parts have been intentionally blurred.

Subscribers Only
Elimination of casts
The following code snippet without generics requires casting (this means you have to
tell the compiler the object is another object):
List list = new ArrayList();
list.add("hello");
String s = (String) list.get(0);
(code was written on my own machine and copied here)
By saying list.get(0) is of type String, you force the ArrayList to be of type String. From
now on, Java will expect the list to only hold Strings. This can be bad if you don’t want
only Strings in your list.
Now let’s see what happens when you use Generics.
List<String> list = new ArrayList<String>();
list.add("hello");
String s = list.get(0); // no cast
Now you don’t need to cast because Java already knows the Arraylist is of type String.
Enabling programmers to implement generic algorithms.
By using generics, programmers can implement generic algorithms that work on
collections of different types, can be customized, and are type safe and easier to read.
Generic Types
A generic type
is a generic class or interface that is parameterized over types. The
following Box class will be modified to demonstrate the concept.
find more resources at oneclass.com
find more resources at oneclass.com
You're Reading a Preview

Unlock to view full version

Subscribers Only