DOC PREVIEW
DREXEL CS 265 - java-new

This preview shows page 1-2-3-4 out of 11 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

New Java FeaturesJava 1.5GenericsExampleChecked CollectionsFor-Each loopAutoboxingSlide 8EnumsSeveral problems without explicit enumsCompareNew Java FeaturesAdvanced Programming TechniquesJava 1.5 Generics For-Each loop Autoboxing Enums Varargs Static import Annotationshttp://java.sun.com/j2se/1.5.0/docs/guide/language/index.htmlGenericsGenerics provides a way to communicate the type of a collection to the compiler, so that it can be checked.The compiler can check that you have used the collection consistently and can insert the correct casts on values being taken out of the collection.Example// Removes 4-letter words from c. Elements must be stringsstatic void expurgate(Collection c) { for (Iterator i = c.iterator(); i.hasNext(); ) if (((String) i.next()).length() == 4) i.remove();}Java 1.4:// Removes the 4-letter words from cstatic void expurgate(Collection<String> c) { for (Iterator<String> i = c.iterator(); i.hasNext(); ) if (i.next().length() == 4) i.remove();}Java 1.5:Checked Collections Automatically generated casts may fail if you accidentally mix types Checked collection wrappers can be used to locate the problem:Set<String> s = Collections.checkedSet(new HashSet<String>(), String.class);For-Each loop Cleaner way to iterate over collections and arraysJava 1.4 Java 1.5int sum(int[] a) { int result = 0; for (int i : a) result += i; return result;}int sum(int[] a) { int result = 0; for (int i=0; i<a.length; i++) result += i; return result;}Autoboxing Collections can only store objects, not primitive data types (int, etc.) When you want to store an int in a collection, you box it using a wrapper class Integer When you get the stored element from the collection, you have to unbox it to get your int back Autoboxing does this for youExample public static void main(String argv[]) { Vector v = new Vector(); v.add(new Integer(1)); v.add(new Integer(2)); for (Iterator i = v.iterator(); i.nextNext(); ) System.out.println(((Integer)i.next()).intValue()); }Java 1.4: public static void main(String argv[]) { Vector<Integer> v = new Vector(); v.add(1); v.add(2); for (int i : v) System.out.println(i); }Java 1.5:Enums Java 1.4 and prior did not support enumerations The standard way to represent an enumerated type: public static final int SEASON_WINTER = 0; public static final int SEASON_SPRING = 1; public static final int SEASON_SUMMER = 2; public static final int SEASON_FALL = 3; What are the problems with this?Several problems without explicit enums Not typesafe No namespace Brittle Printed values are uninformativeCompare enum Season { WINTER, SPRING, SUMMER, FALL } Full-fledged class Comparable Serializable Printed values not


View Full Document

DREXEL CS 265 - java-new

Download java-new
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view java-new and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view java-new 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?