Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.

Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.

Like this presentation? Why not share!

- Java Generics by Zülfikar Karakaya 945 views
- Sun Microsystems Course on AACIMP 2... by SSA KPI 473 views
- Advanced functions in PL SQL by Hosein Zare 524 views
- Java Generics for Dummies by knutmork 2019 views
- Java 8 Programming Tutorial: Lists,... by Marty Hall 946 views
- Java generics final by Akshay Chaudhari 845 views

4,344 views

Published on

No Downloads

Total views

4,344

On SlideShare

0

From Embeds

0

Number of Embeds

95

Shares

0

Downloads

282

Comments

0

Likes

7

No embeds

No notes for slide

and

can still be used, but these will only output carriage return and linefeed. The scanf function that is the inverse of printf has not been included. The Scanner class provides a simple text scanner which can parse primitive types and strings using regular expressions. A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods. In the example shown, the code allows a user to read a number from System.in

- 1. J2SE 5.0 Generics <ul><li>Carol McDonald </li></ul><ul><ul><li>Java Technology Architect </li></ul></ul><ul><ul><ul><li>Sun Microsystems, Inc. </li></ul></ul></ul>
- 2. Speaker’s Qualifications <ul><li>Carol cDonald: </li></ul><ul><ul><li>Java Architect at Sun Microsystems </li></ul></ul><ul><ul><li>Before Sun, worked on software development of: </li></ul></ul><ul><ul><ul><li>Application to manage car Loans for Toyota (>10 million loans) </li></ul></ul></ul><ul><ul><ul><li>Pharmaceutical Intranet ( Roche Switzerland) </li></ul></ul></ul><ul><ul><ul><li>Telecom Network Mgmt ( Digital France) </li></ul></ul></ul><ul><ul><ul><li>X.400 Email Server ( IBM Germany) </li></ul></ul></ul>
- 3. Java SE 1.5 Language Changes
- 4. Seven Major New Features <ul><li>Generics </li></ul><ul><li>Autoboxing/Unboxing </li></ul><ul><li>Enhanced for loop (“foreach”) </li></ul><ul><li>Type-safe enumerations </li></ul><ul><li>Varargs </li></ul><ul><li>Static import </li></ul><ul><li>Metadata </li></ul>
- 5. Generics
- 6. Is there a problem in here? Vector v = new Vector(); v.add(new Integer (4)); OtherClass.expurgate(v); ... static void expurgate(Collection c) { for (Iterator it = c.iterator(); it.hasNext();) if ((( String )it.next()).length() == 4) it.remove(); }
- 7. The Problem (Pre-J2SE 5.0) Vector v = new Vector(); v.add(new Integer (4)); OtherClass.expurgate(v); ... static void expurgate(Collection c) { for (Iterator it = c.iterator(); it.hasNext();) /* ClassCastException */ if ((( String )it.next()).length() == 4) it.remove(); }
- 8. Generics <ul><li>Problem: Collection element types </li></ul><ul><ul><li>Compiler is unable to verify types </li></ul></ul><ul><ul><li>Assignment must use the cast operator </li></ul></ul><ul><ul><li>This can generate runtime errors ( ClassCastException ) </li></ul></ul><ul><li>Solution: </li></ul><ul><ul><li>Tell the compiler what type the collection is </li></ul></ul><ul><ul><li>Let the compiler fill in the cast </li></ul></ul>
- 9. Using Generic Classes <ul><li>Instantiate a generic class to create type specific object </li></ul><ul><li>Example </li></ul><ul><li>Vector <String> x = new Vector <String> (); </li></ul><ul><li>x.add(new Integer(5)); // Compiler error! </li></ul><ul><li>Vector <Integer> y = new Vector <Integer> (); </li></ul>
- 10. Wildcards <ul><li>Method to print contents of any Collection? </li></ul><ul><li>Wrong! </li></ul><ul><li>Passing a Collection of type String will give a compiler error </li></ul>void printCollection(Collection <Object> c) { for (Object o : c) System.out.println( o ); }
- 11. Wildcards <ul><li>Correct way: </li></ul><ul><li>? is the wildcard type </li></ul><ul><li>Collection <?> means Collection of unknown </li></ul>void printCollection( Collection <?> c) { for (Object o : c) System.out.println(o); }
- 12. Bounded Wildcards <ul><li>A wildcard can be specified with an upper bound </li></ul>public void drawAll (List< ? extends Shape >s) { ... } List<Circle> c = getCircles(); drawAll(c) ; List<Triangle> t = getTriangles(); drawAll(t) ;
- 13. Autoboxing & Unboxing
- 14. Autoboxing/Unboxing of Primitive Types <ul><li>Problem: (pre-J2SE 5.0) Conversion between primitive types and wrapper types (and vice-versa) </li></ul><ul><ul><li>must manually convert a primitive type to a wrapper type before adding it to a collection </li></ul></ul><ul><ul><li>int i = 22; </li></ul></ul><ul><ul><li>List l = new LinkedList(); </li></ul></ul><ul><ul><li>l.add( new Integer(i) ); </li></ul></ul>
- 15. Autoboxing/Unboxing of Primitive Types <ul><li>Solution: Let the compiler do it </li></ul><ul><ul><li>Integer intObj = 22 ; // Autoboxing conversion </li></ul></ul><ul><ul><li>int i = intObj; // Unboxing conversion </li></ul></ul><ul><ul><li>ArrayList< Integer > al = new ArrayList<Integer>(); </li></ul></ul><ul><ul><li>al .add( i ); // Autoboxing conversion </li></ul></ul>
- 16. Enhanced for Loop
- 17. Enhanced for Loop (foreach) <ul><li>Problem: (pre-J2SE 5.0) </li></ul><ul><ul><li>Iterating over collections is tricky </li></ul></ul><ul><ul><li>Often, iterator only used to get an element </li></ul></ul><ul><ul><li>Iterator is error prone (Can occur three times in a for loop) </li></ul></ul><ul><li>Solution: Let the compiler do it </li></ul><ul><ul><li>New for loop syntax for (variable : collection) </li></ul></ul><ul><ul><li>Works for Collections and arrays </li></ul></ul>
- 18. Enhanced for Loop Example <ul><li>Old code pre-J2SE 5.0 </li></ul><ul><ul><li>void cancelAll(Collection c) { </li></ul></ul><ul><ul><li>for ( Iterator i = c.iterator(); i.hasNext() ; ){ </li></ul></ul><ul><ul><ul><li>TimerTask task = (TimerTask) i.next(); </li></ul></ul></ul><ul><ul><ul><li>task.cancel(); </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>New Code </li></ul><ul><ul><li>void cancelAll(Collection <TimerTask> c) { </li></ul></ul><ul><ul><ul><li>for ( TimerTask task : c ) </li></ul></ul></ul><ul><ul><ul><li>task.cancel(); </li></ul></ul></ul><ul><ul><li>} </li></ul></ul>Iterating over collections, tricky, error prone <ul><li>New for loop syntax: </li></ul><ul><li>Let the compiler do it </li></ul><ul><li>Works for Collections and arrays </li></ul>
- 19. Type-safe Enumerations
- 20. Type-safe Enumerations <ul><li>Problem: (pre-J2SE 5.0) to define an enumeration: </li></ul><ul><ul><li>Defined a bunch of integer constants: </li></ul></ul><ul><ul><li>public static final int SEASON_WINTER = 0; </li></ul></ul><ul><ul><li>public static final int SEASON_SPRING = 1; </li></ul></ul><ul><li>Issues of using Integer constants </li></ul><ul><ul><li>Not type safe (any integer will pass),Brittleness (how do add value in-between?), Printed values uninformative (prints just int values) </li></ul></ul><ul><li>Solution: New type of class declaration </li></ul><ul><ul><li>enum type has public, self-typed members for each enum constant </li></ul></ul><ul><ul><li>enum Season { WINTER, SPRING, SUMMER, FALL } </li></ul></ul>
- 21. Enumeration Example: public class Card { public enum Suit { spade, diamond, club, heart }; public enum Rank { ace, two, three, four, five, six, seven, eight, nine, ten, jack, queen, king }; private Card (Rank rank, Suit suit) { this.rank = rank; this.suit = suit; } } List< Card > deck = new ArrayList< Card >(); for ( Suit suit : Suit.values ()) for ( Rank rank : Rank.values ()) deck.add(new Card(rank, suit)); Think how much JDK1.4 code this would require!
- 22. Varargs
- 23. Before Varargs Example //example method that takes a variable number of parameters int sum( Integer[] numbers ) { for(int i: numbers) // do something } // Code fragment that calls the sum method sum( new Integer[] {12,13,20} ); http://www.javaworld.com/javaworld/jw-04-2004/jw-0426-tiger1.html <ul><li>Problem: (in pre-J2SE 5.0) To have a method that takes a variable number of parameters </li></ul><ul><ul><li>Can be done with an array, but caller has to create the array first </li></ul></ul>
- 24. Varargs Example (Cont) //example method that takes a variable number of parameters int sum ( Integer... numbers ) { for(int i: numbers) // do something } // Code fragment that calls the sum method sum( 12,13,20 ); http://www.javaworld.com/javaworld/jw-04-2004/jw-0426-tiger1.html <ul><li>Solution: Let the compiler do it for you: </li></ul><ul><ul><li>String format (String fmt, Object... args); </li></ul></ul><ul><ul><li>Java now supports printf(...) </li></ul></ul>
- 25. Varargs examples <ul><li>APIs have been modified so that methods accept variable-length argument lists where appropriate </li></ul><ul><ul><li>Class.getMethod </li></ul></ul><ul><ul><li>Method.invoke </li></ul></ul><ul><ul><li>Constructor.newInstance </li></ul></ul><ul><ul><li>Proxy.getProxyClass </li></ul></ul><ul><ul><li>MessageFormat.format </li></ul></ul><ul><li>New APIs do this too </li></ul><ul><ul><li>System.out. printf (“%d + %d = %d ”, a, b, a+b); </li></ul></ul>
- 26. Static Imports
- 27. Static Imports <ul><li>Problem: (pre-J2SE 5.0) </li></ul><ul><ul><li>Having to fully qualify every static referenced from external classes </li></ul></ul><ul><li>Solution: New import syntax </li></ul><ul><ul><li>import static TypeName.Identifier; </li></ul></ul><ul><ul><li>import static Typename.*; </li></ul></ul><ul><ul><li>Also works for static methods and enums </li></ul></ul><ul><ul><li>e.g Math.sin(x) becomes sin(x) </li></ul></ul>
- 28. Formatted I/O
- 29. Simple Formatted I/O <ul><li>Printf is popular with C/C++ developers </li></ul><ul><ul><li>Powerful, easy to use </li></ul></ul><ul><li>Finally adding printf to J2SE 5.0 (using varargs) </li></ul><ul><ul><li>out.printf(“%-12s is %2d long”, name, l); </li></ul></ul><ul><ul><li>out.printf(“value = %2.2F”, value); </li></ul></ul>
- 30. Annotations
- 31. Annotations Metadata (JSR-175) <ul><li>Provide standardised way of adding annotations to Java code </li></ul><ul><ul><li>public @Remote void foo() {} </li></ul></ul><ul><li>Annotations are used by tools that work with Java code: </li></ul><ul><ul><li>Compiler </li></ul></ul><ul><ul><li>IDE </li></ul></ul><ul><ul><li>Runtime tools </li></ul></ul><ul><li>Used to generate interfaces, deployment descriptors... </li></ul>
- 32. Annotations Example: JAX-RPC <ul><li>Old Code </li></ul><ul><li>public interface PingIF implements java.rmi.Remote { </li></ul><ul><ul><li>public void foo() throws java.rmi.RemoteException; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>public class Ping implements PingIF { </li></ul></ul><ul><ul><li>public void foo() {...} </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>New Code </li></ul><ul><ul><li>public class Ping { </li></ul></ul><ul><ul><li>public @Remote void foo() {} </li></ul></ul><ul><ul><li>} </li></ul></ul>
- 33. Resources and Summary
- 34. For More Information (1/2) <ul><li>Memory management white paper </li></ul><ul><ul><li>http://java.sun.com/j2se/reference/whitepapers/ </li></ul></ul><ul><li>Destructors, Finalizers, and Synchronization </li></ul><ul><ul><li>http://portal.acm.org/citation.cfm?id=604153 </li></ul></ul><ul><li>Finalization, Threads, and the Java Technology Memory Model </li></ul><ul><ul><li>http://developers.sun.com/learning/javaoneonline/2005/coreplatform/TS-3281.html </li></ul></ul><ul><li>Memory-retention due to finalization article </li></ul><ul><ul><li>http://www.devx.com/Java/Article/30192 </li></ul></ul>
- 35. For More Information (2/2) <ul><li>FindBugs </li></ul><ul><ul><li>http://findbugs.sourceforge.net </li></ul></ul><ul><li>Heap analysis tools </li></ul><ul><ul><li>Monitoring and Management in 6.0 </li></ul></ul><ul><ul><ul><li>http://java.sun.com/developer/technicalArticles/J2SE/monitoring/ </li></ul></ul></ul><ul><ul><li>Troubleshooting guide </li></ul></ul><ul><ul><ul><li>http://java.sun.com/javase/6/webnotes/trouble/ </li></ul></ul></ul><ul><ul><li>JConsole </li></ul></ul><ul><ul><ul><li>http://java.sun.com/developer/technicalArticles/J2SE/jconsole.html </li></ul></ul></ul>
- 36. Resources <ul><li>http://java.sun.com/javase </li></ul><ul><li>Java.net </li></ul><ul><li>http://jdk.dev.java.net </li></ul>
- 37. Stay in Touch with Java SE <ul><li>http://java.sun.com/javase </li></ul><ul><li>JDK Software Community </li></ul><ul><ul><li>planetjdk.org </li></ul></ul><ul><ul><li>community.java.net/jdk </li></ul></ul><ul><li>JDK 6 </li></ul><ul><ul><li>http://jdk6.dev.java.net/ </li></ul></ul><ul><ul><li>http://jcp.org/en/jsr/detail?id=270 </li></ul></ul><ul><li>JDK 7 </li></ul><ul><ul><li>http://jdk7.dev.java.net/ </li></ul></ul><ul><ul><li>http://jcp.org/en/jsr/detail?id=277 </li></ul></ul>
- 38. Thank You! <ul><li>Carol McDonald </li></ul><ul><ul><li>Java Technology Architect </li></ul></ul><ul><ul><ul><li>Sun Microsystems, Inc. </li></ul></ul></ul>

No public clipboards found for this slide

×
### Save the most important slides with Clipping

Clipping is a handy way to collect and organize the most important slides from a presentation. You can keep your great finds in clipboards organized around topics.

Be the first to comment