Data Types and Exceptions in Java
Jim White
Java Instructor
Two categories of types in Java:
java.util
packageArrays | Collections |
---|---|
Not resizable | Dynamically sized (grow and shrink) |
Stores primitives or objects | Stores only objects |
Homogeneous - elements must be the same | heterogeneous - objects can be different |
Special notation to access [] elements |
Uses methods to access objects |
Special syntax for initialization {} |
Use new (no special initialization syntax) |
Collection
and Map
java.util.Collection
& java.util.Map
import java.util.*;
to use any of the Collections Framework typesCollection
defines many kinds of groupings of objectsList
Set
Queue
.add(Object)
and .remove(Object)
methods to modify any Collection.put(Object key, Object value)
to add to a Map.remove(Object key)
to remove from a MapHashMap
Collection
or Map
<Class>
with a parameterized type.< >
called the diamond operatorArrayList<String> list = new ArrayList<String>(); // Construct with generics
Collection
and Map
ArrayList<String> list = new ArrayList<String>();
list.add("hello"); // Adding a String is ok
list.add(new Integer(5)); // Trying to add an Integrer causes compiler error
ArrayList list2 = new ArrayList(); // Legal but non-specific
list2.add("hello"); // Now any type of object can be added
list2.add(new Integer(5));
ArrayList<String> list = new ArrayList<String>(); //Single line declaration and assignment
ArrayList<String> list2; // Variable declaration...
list2 = new ArrayList<String>(); //... and assignement can be 2 statements
Data Types and Exceptions in Java