1 / 16

Java 1.5 Language Features: IO, Enum, Wrapper, Enhanced For Loop

This article explores the new language features introduced in Java 1.5, including input/output handling, enum types, wrapper classes, and the enhanced for loop.

susiemartin
Download Presentation

Java 1.5 Language Features: IO, Enum, Wrapper, Enhanced For Loop

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS202 Java Object Oriented ProgrammingNew Language Features in Java 1.5 Chengyu Sun California State University, Los Angeles

  2. Ten Things I Hate About You • The Input/Output Woes • The Ugly Cast • The Annoying Wrapping/Unwrapping • No ENUM??

  3. Input import javax.swing.JOptionPane; ... ... String s = JOptionPane.showInputDialog(null, “An integer please:”); int n = Integer.parseInt( s ); ConsoleReader in = new ConsoleReader(); int n = in.readInt();

  4. Output import java.text.DecimalFormat; ... ... System.out.println( 5 ); DecimalFormat f = new DecimalFormat( “.###” ); System.out.println( f.format(5.0/3.0) ); In C: printf( “%.3f”, 5.0/3.0 );

  5. Cast ArrayList names = new ArrayList(); names.add( “Peter Parker” ); names.add( “Mary Jane Watson” ); ... ... // print out a list of last names for( int i=0 ; i < names.size() ; ++i ) { String s = (String) names.get(i); System.out.println( s.substring(s.lastIndexOf(‘ ’)) ); }

  6. Wrapping and Unwrapping ArrayList numbers = new ArrayList(); numbers.add( new Integer(10) ); numbers.add( new Integer(22) ); ... ... // calculate the sum of all numbers int sum = 0; for( int i=0 ; i < numbers ; ++i ) sum += ((Integer) numbers.get(i)).intValue();

  7. ENUM int dayOfWeek; • A day of week can only be one of the seven values: MON, TUE, WED, THR, FRI, SAT, SUN public static final int SUN = 0; public static final int MON = 1; public static final int TUE = 2; public static final int WED = 3; public static final int THR = 4; public static final int FRI = 5; public static final int SAT = 6; and it still doesn’t prevent somebody assign –1 to day_of_week

  8. Java 1.5 Improvements • Scanner class • Formatted output • Generic types • Auto-boxing/unboxing • enum

  9. Scanner Class Scanner s = new Scanner(System.in); String param = s.next(); int value = s.nextInt(); s.close(); double sum = 0; Scanner s2 = new Scanner( new File(“test”) ); while( s2.hasNext() ) sum += s2.nextDouble();

  10. Formatted Output • ... with added bonus of “%n” System.out.printf( “%d, %3.4f%n”, 10, 4.0/3 );

  11. Generic Type • Type parameter ArrayList<Integer> list = new ArrayList<Integer>(); list.add(new Integer(42)); int total = list.get(0).intValue();

  12. Auto-Boxing/Unboxing ArrayList<Integer> numbers = new ArrayList<Integer>(); numbers.add(10); numbers.add(22); ... ... // calculate the sum of all numbers int sum = 0; for( int i=0 ; i < numbers ; ++i ) sum += numbers.get(i);

  13. Enumerated Types public enum DayOfWeek { sun, mon, tue, wed, thr, fri, sat }; DayOfWeek dayOfWeek; dayOfWeek = sun; dayOfWeek = 1; // compilation error!

  14. Other Goodies – Enhanced For Loop • Before • ArrayList<Integer> list = new ArrayList<Integer>(); • Iterator i = list.iterator(); • while( i.hasNext() ) { • Integer value = (Integer) i.next(); • ... ... • } • After • ArrayList<Integer> list = new ArrayList<Integer>(); • for (Integer i : list) { ... }

  15. Other Goodies – Variable Arguments void argtest ( Object ... args ) { for( int i=0 ; i <args.length ; i++ ) System.out.println( args[i] ); } ... ... argtest("test", "data");

  16. Additional Readings • J2SE 1.5 in a Nutshell – http://java.sun.com/developer/technicalArticles/releases/j2se15/ • J2SDK 1.5.0 API documentation – http://java.sun.com/j2se/1.5.0/docs/api/overview-summary.html

More Related