24-Oct-15 Java 1.5. 2 Java 1.5.0 beta 1 (“Tiger”) Released February 4, 2004 Themes Ease of Development Scalability.

Slides:



Advertisements
Similar presentations
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Advertisements

9-Jun-14 Enum s (and a review of switch statements)
1 CSE 331 Enumerated types ( enum ) slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.
New features in JDK 1.5 Can these new and complex features simplify Java development?
Java Review Interface, Casting, Generics, Iterator.
Written by: Dr. JJ Shepherd
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.
CHAPTER 12 GENERICS Introduction to java 1. Assignment 5 Solution See Eclipse.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
J2SE 5.0 New Features. J2SE 5.0 aka JDK 1.5 aka Tiger.
30-Jun-15 Generics. Arrays and collections In Java, array elements must all be of the same type: int[] counts = new int[10]; String[] names = { "Tom",
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.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
P Object type and wrapper classes p Object methods p Generic classes p Interfaces and iterators Generic Programming Data Structures and Other Objects Using.
Chapter 21 Generics 1. Generics - Overview Generic Methods specify a set of related methods Generic classes specify a set of related types Software reuse.
07 Coding Conventions. 2 Demonstrate Developing Local Variables Describe Separating Public and Private Members during Declaration Explore Using System.exit.
Effective Java: Generics Last Updated: Spring 2009.
The Java Programming Language
GENERIC COLLECTIONS. Type-Wrapper Classes  Each primitive type has a corresponding type- wrapper class (in package java.lang).  These classes are called.
1162 JDK 5.0 Features Christian Kemper Principal Architect Borland.
ArrayList, Multidimensional Arrays
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Chapter 18 Java Collections Framework
Java 5 Part 1 CSE301 University of Sunderland Harry Erwin, PhD.
Java development environment and Review of Java. Eclipse TM Intergrated Development Environment (IDE) Running Eclipse: Warning: Never check the “Use this.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
1 Generics Chapter 21 Liang, Introduction to Java Programming.
CompSci 100E 40.1 Java 5 New Features  Generics  Enhanced for loop  Autoboxing/unboxing  Typesafe enums  Other  Varargs  Static Import  Metadata.
Objects First With Java A Practical Introduction Using BlueJ Supplementary Material for Java
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Java 5 Part 2 CSE301 University of Sunderland Harry Erwin, PhD.
CMSC 132: Object-Oriented Programming II Java Constructs Department of Computer Science University of Maryland, College Park.
This recitation 1 An interesting point about A3: Using previous methods to avoid work in programming and debugging. How much time did you spend writing.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
Georgia Institute of Technology What is new in Java 5.0 (1.5)? Barb Ericson Georgia Institute of Technology June 2006.
©SoftMoore ConsultingSlide 1 Generics “Generics constitute the most significant change in the Java programming language since the 1.0 release.” – Cay Horstmann.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Recitation 5 Enums and The Java Collections classes/interfaces 1.
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
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
1 CSC 2053 New from AutoBoxing 3 Before J2SE 5.0, working with primitive types required the repetitive work of converting between the primitive.
CSE 1201 Object Oriented Programming ArrayList 1.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
Written by: Dr. JJ Shepherd
Bart van Kuik Application Developer Oracle Corporation.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
1 Chapter 21 Generics. 2 Objectives F To use generic classes and interfaces (§21.2). F To declare generic classes and interfaces (§21.3). F To understand.
Sixth Lecture ArrayList Abstract Class and Interface
Review of Java … or maybe not.
Introduction to Computer Science / Procedural – 67130
Some random things in Java
Week 6 Discussion Word Cloud.
Starting JavaProgramming
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
(and a review of switch statements)
(and a review of switch statements)
Units with – James tedder
Units with – James tedder
Generics, Lambdas, Reflections
Java 5 New Features 1-May-19.
slides created by Ethan Apter and Marty Stepp
Presentation transcript:

24-Oct-15 Java 1.5

2 Java beta 1 (“Tiger”) Released February 4, Themes Ease of Development Scalability and Performance Monitoring and Manageability Desktop Client Miscellaneous Features To use Java 1.5: Download and install from Use the -source 1.5 flag Optionally, get: NetBeans 3.6, or......get Eclipse 3.0 build M 8 and the JDK 15. plugin

3 Generic types Up through Java 1.4, collections hold arbitrary Object s If you wanted, say, a Vector of Employee s, you had two basic choices: Use a Vector Adding an Employee is easy (but so is adding any other Object, however inappropriate) Getting an Employee from the Vector usually requires a cast, which may result in a runtime error Extend Vector with an EmployeeVector class Checking is done at compile time (which is better than runtime) It’s a lot of extra work Generic types do the second of these automatically for you Syntax: Vector employees = new Vector ();

4 An example Instead of: ArrayList list = new ArrayList(); list.add(0, new Integer(42)); int total = ((Integer)list.get(0)).intValue(); You can say: ArrayList list = new ArrayList (); list.add(0, new Integer(42)); int total = list.get(0).intValue(); Advantages: Less work to create specialized data structures Checking done at compile time rather than runtime For C++ programmers, the syntax is familiar Disadvantage: Yet more ugly syntax to learn

5 More generics A lot of other things, besides collections, have been “genericized” in Java 1.5 Parameterized Type Vector stringVector = new Vector List integerList = new List Interface interface List implements MyInterface{...} Class class MyList {...} class MyList implements List {...} Method boolean containsBoth(Element a, Element b); static boolean swap(List list, int i, int j);

6 Autoboxing Up through Java 1.4, primitive types cannot be used where an object is required They had to be wrapped, or boxed, for instance: myStack.push(new Integer(25)); Primitive types still cannot be used where an object is required… …But Java 1.5 now does this conversion automatically: Now you can say myStack.push(25); What happens at runtime is still the same It’s just that the compiler does the conversion for you

7 Auto-unboxing Just as you could not use primitive types where an Object was required, you could not use an Object where a primitive type was required You had to do your own unwrapping, or un-boxing For instance, you had to write code such as: int result = ((Integer)myStack.pop()).intValue(); Now you can just write: int result = myStack.pop(); Even more striking, myStack.push(new Integer(((Integer)myStack.pop()).intValue() + ((Integer)myStack.pop()).intValue())); becomes myStack.push(myStack.pop() + myStack.pop());

8 Combining generics with autoboxing Instead of: ArrayList list = new ArrayList (); list.add(0, new Integer(42)); int total = list.get(0).intValue(); You can say: ArrayList list = new ArrayList (); list.add(0, 42); int total = list.get(0); Advantages: Less code to write Code becomes more readable

9 Auto [un]boxing disadvantages Since more is hidden, it becomes harder to understand and explain what is going on Equality is no longer transitive! Consider: Integer a = new Integer(7); int b = 7; Integer c = new Integer(7); if (a == b) … // will be true if (b == c) … // will be true if (a == c) … // will be false For Objects, == is an identity test This is a dangerous trap for the unwary

10 New for loop Many collections in Java use Iterator s The new for loop automatically uses an Iterator Example Java 1.4 code: for (Iterator iter = employeeList.iterator(); iter.hasNext(); ) { Employee employee =(Employee)iter.next(); System.out.println(employee.getName()); } Example Java 1.5 code: for (Employee employee : employeeList) { System.out.println(employee.getName()); } Advantage: Convenient and fairly easy to read Also works for iterators you define Disadvantage: Stupid syntax (I wish they had used “ in ”)

11 Enumerated types Enumerated types are a way of representing nonstandard data, such as days of the week, with integers: static final int SUNDAY = 1; static final int MONDAY = 2; … Or months of the year: static final int JANUARY = 0; static final int FEBRUARY = 1; … The problem with this approach is that there are so many ways to make errors: static final int DECEMBER = 12; int date = year + month + dayOfWeek; Over the past few years there have been many attempts to define an idiom for making type-safe enumerations

12 Enumerated types in Java 1.5 public enum Suit {clubs, diamonds, hearts, spades} Notice new keyword “ enum ” Values of enum constants are assigned automatically public enum Coin{ penny(1), nickel(5), dime(10), quarter(25); // values Coin(int value) { this.value = value; } // constructor private final int value; // instance variable public int value() { return value; } // method } This is very like a class declaration Printing results in the named constant: dime, not 10 Named constants can be read in

13 Using an enumerated type public class CoinTest { public static void main(String[] args) { for (Coin c : Coin.VALUES) System.out.println(c + ": \t" + c.value() +"¢ \t" + color(c)); } private enum CoinColor { copper, nickel, silver } private static CoinColor color(Coin c) { switch(c) { case Coin.penny: return CoinColor.copper; case Coin.nickel: return CoinColor.nickel; case Coin.dime: // deliberate fall through to next case case Coin.quarter: return CoinColor.silver; default: throw new AssertionError("Unknown coin: " + c); } } }

14 Advantages of enumerated types enum types simplify code and make it more readable Types are checked at compile time (hence, very type safe) Performance is comparable to int constants Each enum type has its own name space (you don't have to say Coin.dime ) You can add, reorder or even remove constants without the need to recompile other classes that use these constants I’ve seen one source that says you do need to recompile Printed values are informative enum constants can be used in collections (for example, as HashMap keys) You can add arbitrary fields and methods to an enum class An enum type can be made to implement arbitrary interfaces enum types can be used in switch statements enum types are serializable

15 Disadvantages of enumerated types Still more complexity in the language Harder for beginners, nicer for experts!

16 The “Constant Interface” antipattern public interface Physics { public static final double AVOGADROS_NUMBER = e23; public static final double BOLTZMANN_CONSTANT = e-23; public static final double ELECTRON_MASS = e-31; } public class Guacamole implements Physics { public static void main(String[] args) { double moles =...; double molecules = AVOGADROS_NUMBER * moles;... } } This is considered to be very poor style by smart programmers who have thought about it a lot more than I have

17 Static import You can now import just the static members of a class or interface public interface Physics { // as before public static final double AVOGADROS_NUMBER = e23; public static final double BOLTZMANN_CONSTANT = e-23; public static final double ELECTRON_MASS = e-31; } import static org.iso.Physics.*; class Guacamole { public static void main(String[] args) { double molecules = AVOGADROS_NUMBER * moles;... } }

18 Metadata Metadata lets you avoid writing boilerplate code, by enabling tools to generate it from annotations in the source code This leads to a “declarative” programming style where the programmer says what should be done, and tools emit the code to do it Details are tool-dependent, but the syntax involves the use of sign I have not yet seen any good, understandable examples

19 Varargs If you want to pass a variable number of arguments to a method, you currently have to put them into an array This is a lot of extra, messy code Java 1.5 allows varargs—a variable number of arguments Example: public String format(String pattern, Object... args) {...} The ellipsis (... ) after Object is the new syntax The varargs must be the very last thing in the parameter list Within the method, args has type Object[ ] No extra syntax is needed within the method— args is just an array No new syntax is needed to call—just supply the parameters you want

20 Improved output The new Formatter class, modeled after C’s printf statement, gives you much simpler formatted output This class provides support for: Layout justification and alignment Common formats for numeric, string, and date/time data Locale-specific output Common Java types such as byte, BigDecimal, and Calendar Limited formatting customization for arbitrary user types is provided through the Formattable interface

21 Improved input The new Scanner class can parse primitive types and strings using regular expressions The Scanner class can also read in input from a String (not just an I/O stream) Thankfully, this class was not modeled after C’s scanf !

22 Other stuff Lots of new classes and interfaces, such as Queue and PriorityQueue, MouseInfo More “looks and feels:” Synth and Ocean Enhancements to sound and image processing Improvements in tools ( javac, javadoc, JVMTI, JPDA ) Other improvements in networking, security, internationalization, etc. Bug fixes (of course)

23 Final comments As I said at the beginning of CIT591, the half-life of knowledge in computer science is about 5 years You are well-prepared to learn Java 1.5, since it’s mostly “more of the same” I expect to convert to Java 1.5 as soon as classes are over, and use it in the Fall

24 The End “Well, in our country,” said Alice, panting a little, “You’d generally get somewhere else if you ran very fast for a long time, as we’ve been doing.” “A slow sort of country!” said the Queen. “Now here, you see, it takes all the running you can do to keep in the same place. If you want to get somewhere else, you must run at least twice as fast as that!” —Lewis Carroll, Alice in Wonderland