Java 5 New Features 1-May-19.

Slides:



Advertisements
Similar presentations
9-Jun-14 Enum s (and a review of switch statements)
Advertisements

1-May-15 Java 1.5. Reason for changes “The new language features all have one thing in common: they take some common idiom and provide linguistic support.
11-Jun-15 Generics. A generic is a method that is recompiled with different types as the need arises The bad news: Instead of saying: List words = new.
03 Data types1June Data types CE : Fundamental Programming Techniques.
Compilation 2007 Java 1.5 Features Michael I. Schwartzbach BRICS, University of Aarhus.
Java 1.5 & Effective Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
J2SE 5.0 New Features. J2SE 5.0 aka JDK 1.5 aka Tiger.
15-Jul-15 Generics. ArrayList s and arrays A ArrayList is like an array of Object s, but... Arrays use [ ] syntax; ArrayList s use object syntax An ArrayList.
16-Jul-15 Java Versions of Java Java 1 Java 2 Java 5.0 Oak: Designed for embedded devices Java 1.1: Adds inner classes and a completely new event-handling.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Effective Java: Generics Last Updated: Spring 2009.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
1162 JDK 5.0 Features Christian Kemper Principal Architect Borland.
24-Oct-15 Java Java beta 1 (“Tiger”) Released February 4, Themes Ease of Development Scalability.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Java 5 Part 1 CSE301 University of Sunderland Harry Erwin, PhD.
CompSci 100E 40.1 Java 5 New Features  Generics  Enhanced for loop  Autoboxing/unboxing  Typesafe enums  Other  Varargs  Static Import  Metadata.
1 Enumerations and Structs Chapter 9. 2 Objectives You will be able to: Write programs that define and use enumeration variables. Write programs that.
Java 1.5 The New Java Mike Orsega Central Carolina CC.
Objects First With Java A Practical Introduction Using BlueJ Supplementary Material for Java
Java 5 Part 2 CSE301 University of Sunderland Harry Erwin, PhD.
©SoftMoore ConsultingSlide 1 Generics “Generics constitute the most significant change in the Java programming language since the 1.0 release.” – Cay Horstmann.
Object Oriented Programming in Java Lecture 14. Review Quiz 1. Write a method which gets an Object and call all its available ‘get’ Methods and print.
11-Jan-16 Bits and Pieces Some random things in Java.
Enterprise Java Java SE 5 TM Language Feature Enhancement Primary Source: New Features and Enhancements J2SE 5.0
1 CSC 2053 New from AutoBoxing 3 Before J2SE 5.0, working with primitive types required the repetitive work of converting between the primitive.
Bart van Kuik Application Developer Oracle Corporation.
© 2004 Pearson Addison-Wesley. All rights reserved January 27, 2006 Formatting Output & Enumerated Types & Wrapper Classes ComS 207: Programming I (in.
New Java Features Advanced Programming Techniques.
COP Topics Java Basics - How to write & call your own method - Math methods (static methods) - String methods (instance methods) - Errors Classes.
Sun Tech Talk 2 Java 1.5 and NetBeans 5.0 Pierre de Filippis Sun Campus Evangelist
Seven Lecture Collection 1. 2 Some Legacy Collection Types  ArrayList  Enumeration  Vector  HashTable – self study.
SCJP 5, 1/7 Declarations, Initialization and Scoping
OOP Tirgul 12.
Java 1.5 New Features 22-Apr-18.
Topic: Classes and Objects
Computer Organization and Design Pointers, Arrays and Strings in C
Intro to Java L. Grewe.
Sixth Lecture ArrayList Abstract Class and Interface
Formatting Output & Enumerated Types & Wrapper Classes
From C to C++.
Software Construction
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
ADT’s, Collections/Generics and Iterators
From C to C++.
Some random things in Java
Java Programming: From Problem Analysis to Program Design, 4e
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Generics A Brief Review 16-Nov-18.
More inheritance, Abstract Classes and Interfaces
Some random things in Java
Generics 27-Nov-18.
(and a review of switch statements)
(and a review of switch statements)
Chapter 2: Basic Elements of Java
Some random things in Java
Arrays and Collections
Barb Ericson Georgia Institute of Technology June 2006
Arrays and Array Lists CS 21a.
CSC 221: Computer Programming I Fall 2009
ArrayLists 22-Feb-19.
Week 4 Lecture-2 Chapter 6 (Methods).
Generics, Lambdas, Reflections
COP 3330 Notes 4/6.
Some random things in Java
slides created by Alyssa Harding
Outline Creating Objects The String Class The Random and Math Classes
Generics 2-May-19.
Visibilities and Static-ness
Presentation transcript:

Java 5 New Features 1-May-19

New features (Not all) Generics Enhanced for loop Autoboxing/unboxing Compile-time type safety for collections without casting Enhanced for loop Eliminates the drudgery and error-proneness of iterators Autoboxing/unboxing Avoids manual conversion between primitive types (such as int) and wrapper types (such as Integer) Typesafe enums Provides all the well-known benefits of the Typesafe Enum pattern Static import Lets you avoid qualifying static members with class names Metadata Tools to generate boilerplate code from annotations in the source code Leads to a "declarative" programming style where the programmer says what should be done and tools emit the code to do it

Compiler Sugar Compiler helps you That is, you can key in less words But it does not actually extend the power of the language itself! Many new features are provided by compiler sugars, but not all!

Generics A generic is a method that is recompiled with different types as the need arises The bad news: Instead of saying: List words = new ArrayList(); You'll have to say: List<String> words = new ArrayList<String>(); The good news: Provides compile-time checking to make sure you are using the correct type No casting; instead of String title = ((String) words.get(i)).toUppercase(); you use String title = words.get(i).toUppercase();

Enhanced for loop Instead of void cancelAll(Collection c) { for (Iterator i = c.iterator(); i.hasNext(); ) { TimerTask tt = (TimerTask) i.next(); tt.cancel(); } } You will be able to use: void cancelAll(Collection c) { for (Object o : c) ((TimerTask)o).cancel(); } Or: void cancelAll(Collection<TimerTask> c) { for (TimerTask task : c) task.cancel(); } Not everyone likes this syntax!

Autoboxing Java won’t let you use a primitive value where an object is required--you need a “wrapper” Similarly, you can’t use an object where a primitive is required--you need to “unwrap” it Java 1.5 makes this automatic: Map<String, Integer> m = new TreeMap<String, Integer>(); for (String word : args) { m.put(word, m.get(word) + 1); }

Enumerations An enumeration, or “enum,” is simply a set of constants to represent various values Here’s the old way of doing it public final int SPRING = 0; public final int SUMMER = 1; public final int FALL = 2; public final int WINTER = 3; This is a nuisance, and is error prone as well Here’s the new way of doing it: enum Season { winter, spring, summer, fall }

Advantages of the new enum They provide compile-time type safety int enums don't provide any type safety at all They provide a proper name space for the enumerated type With int enums you have to prefix the constants to get any semblance of a name space. They're robust int enums are compiled into clients, and you have to recompile clients if you add, remove, or reorder constants. Printed values are informative If you print an int enum you just see a number. Because they're objects, you can put them in collections. Because they're essentially classes, you can add arbitrary fields and methods

New features of enum public enum Coin { PENNY(1), NICKEL(5), DIME(10), QUARTER(25); Coin(int value) { this.value = value; } private final int value; public int value() { return value; } }

Another Interesting Example public enum Operation { PLUS { double eval(double x, double y) { return x + y; } }, MINUS { double eval(double x, double y) { return x - y; } }, TIMES { double eval(double x, double y) { return x * y; } }, DIVIDE { double eval(double x, double y) { return x / y; } }; // Do arithmetic op represented by this constant abstract double eval(double x, double y); }

public static void main(String args[]) { double x = Double public static void main(String args[]) { double x = Double.parseDouble(args[0]); double y = Double.parseDouble(args[1]); for (Operation op : Operation.values()) System.out.printf("%f %s %f = %f%n", x, op, y, op.eval(x, y)); } $ java Operation 4 2 4.000000 PLUS 2.000000 = 6.000000 4.000000 MINUS 2.000000 = 2.000000 4.000000 TIMES 2.000000 = 8.000000 4.000000 DIVIDE 2.000000 = 2.000000 怎麼改成印+-*/

Static import facility import static java.lang.System.out; public class Test { public static void main(String...args) { out.println("hello world"); } You no longer have to say System.out.println

scanf & printf Scanner sc = new Scanner(System.in); int i = sc.nextInt(); String name = sc.next(); System.out.printf("[%s]我今年%d歲", "popcorny", 18); String str = String.format("[%s]屁啦~", "moliwang");

Variable length parameters public static void main(String[] args) public static void main(String… args)

Metadata @Override To guarantee you “override” a method. That is, typo is not possible

Reference http://java.sun.com/j2se/1.5.0/docs/relnotes/features.html (All new features, hard) http://www.cs.indiana.edu/classes/jett/sstamm/