New features in JDK 1.5 Can these new and complex features simplify Java development?

Slides:



Advertisements
Similar presentations
Review Generics and the ArrayList Class
Advertisements

Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
9-Jun-14 Enum s (and a review of switch statements)
CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7.9Arrays of Pointers Arrays can contain pointers For.
Classes and Objects. What is Design? The parts of the software including – what information each part holds – what things each part can do – how the various.
COSC 2006 Data Structures I Instructor: S. Xu URL:
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Java Review Interface, Casting, Generics, Iterator.
Objects, Variables & Methods Java encapsulates data and action modules that access the data in one container, called an object. Object members that.
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.
Generic programming in Java
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
Java Generics.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
Java Generics. 2 The Dark Ages: Before Java 5 Java relied only on inclusion polymorphism  A polymorphism code = Using a common superclass Every class.
Java 1.5 & Effective Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
J2SE 5.0 New Features. J2SE 5.0 aka JDK 1.5 aka Tiger.
1 Chapter 21 Generics. 2 Objectives F To know the benefits of generics (§21.1). F To use generic classes and interfaces (§21.2). F To declare generic.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 17 Animated Version Generics and Type Safety.
CSS446 Spring 2014 Nan Wang.  To learn how to choose appropriate classes for a given problem  To understand the concept of cohesion  To minimize dependencies.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
© Amir Kirsh Object Oriented Programming with Java Written by Amir Kirsh.
Java Generics.
Generics In Java 1.5 By Manjunath Beeraladinni. Generics ➲ New feature in JDK1.5. ➲ Generic allow to abstract over types. ➲ Generics make the code clearer.
Distributed Systems (236351) Tutorial 1 - Getting Started with Visual Studio C#.NET.
Chapter 21 Generics 1. Generics - Overview Generic Methods specify a set of related methods Generic classes specify a set of related types Software reuse.
Effective Java: Generics Last Updated: Spring 2009.
Generics1 Parametrized classes and methods. Generics2 What are generics Generics are classes or interfaces that can be instantiated with a variety of.
Generics1 Parametrized classes and methods. Generics2 What are generics Generics are classes or interfaces that can be instantiated with a variety of.
Generics1 Parametrized classes and methods. Generics2 What are generics Generics are classes or interfaces that can be instantiated with a variety of.
A Singleton Puzzle: What is Printed? 1 public class Elvis { public static final Elvis INSTANCE = new Elvis(); private final int beltSize; private static.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
1162 JDK 5.0 Features Christian Kemper Principal Architect Borland.
Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
Java 5 Part 1 CSE301 University of Sunderland Harry Erwin, PhD.
Introduction to Computer Science 2 Slide 1 Enumerated types We already know some data types int, float, char good for problems that involve int,
1 Generics Chapter 21 Liang, Introduction to Java Programming.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
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.
Effective Java – General Programming Global Server Dept. Hu Qiongkai
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 19 Generics.
©SoftMoore ConsultingSlide 1 Generics “Generics constitute the most significant change in the Java programming language since the 1.0 release.” – Cay Horstmann.
GENERICS AND THE JAVA COLLECTIONS FRAMEWORK Lecture 16 CS2110 – Fall 2015 Photo credit: Andrew Kennedy.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Java Generics. It is nice if we could write a single sort method that could sort array of any type of elements: – Integer array, – String array, Solution:
Recitation 5 Enums and The Java Collections classes/interfaces 1.
1 CSC 2053 New from AutoBoxing 3 Before J2SE 5.0, working with primitive types required the repetitive work of converting between the primitive.
CS 61B Data Structures and Programming Methodology July 8, 2008 David Sun.
Introduction to C# By: Abir Ghattas Michel Barakat.
Bart van Kuik Application Developer Oracle Corporation.
 2000 Prentice Hall, Inc. All rights reserved Introduction Structures –Collections of related variables (aggregates) under one name Can contain.
StructureStructure. Outline Introduction Structure Definitions Initializing Structures Accessing Members of Structures Using Structures with Functions.
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.
OOP Tirgul 10. What We’ll Be Seeing Today  Generics – A Reminder  Type Safety  Bounded Type Parameters  Generic Methods  Generics and Inner Classes.
Introduction to Enumerations CMSC 202. Enumerated Values Enumerated values are used to represent a set of named values. Historically in Java (and other.
Intro to Java L. Grewe.
More on Java Generics Multiple Generic Types Bounded Generic Types
Generics, Lambdas, Reflections
Object Based Programming
Conditional Statements
More About Objects and Methods
Generics, Lambdas, Reflections
Chapter 19 Generics.
Presentation transcript:

New features in JDK 1.5 Can these new and complex features simplify Java development?

Overview Generic Types Auto-boxing/unboxing Enhanced For Loop Enumerations Variable Arguments Static Import Meta-data Formatted I/O Concurrency Utilities Management Utilities Class Data Sharing Loads of other improvements

Reasoning "The new language features all have one thing in common: they take some common idiom and provide linguistic support for it. In other words, they shift the responsibility for writing the boilerplate code from the programmer to the compiler." - Joshua Bloch, senior staff engineer, Sun Microsystems

Complementary Features Generics improve enhanced for loop Generics and auto-boxing/unboxing allow Enumerated types Variable args improve API usability Variable args allow formatted I/O

New features enhance existing API Generics enhance compile time type checking Generics improve reflection Generics allow return type overriding Enum improves readability and organization of constants Static import improves utility functions Auto-boxing improves readability

Generic Types interface Collection { boolean add(E o); boolean addAll(Collection c); Iterator iterator(); T[] toArray(T[] a); … }

Generic Interface (or Class) interface Collection … Collection integers; E is the type parameter for the generic type Collection is the raw type after type erasure Integer is the actual type parameter used when declaring an instance of the generic type Cannot use primitive types but auto-boxing covers over the difference

Using the Type Parameter boolean add(E o); Where ever E is used, the compiler will check the type against the actual type parameter. At runtime E will be Object.

Wildcard Parameters boolean addAll(Collection c); ? represents an unknown type List list = new ArrayList (); Not legal: List list = new ArrayList (); Would allow list.add( new Object() );

Bounded Type Parameters boolean addAll(Collection c); Constrains possible actual types An actual type must be same as E or extend E Can also constrain to implement multiple interfaces

Parameterized Methods T[] toArray(T[] a); Infers the actual type from the actual parameter passed to the method.

Type Erasure Insures backwards compatibility Avoids bloat ala C++ templates Type information still available through reflection

Small Example public static void main ( String... args ) { Collection strs = new ArrayList (); for ( String s : args ) strs.add( s ); Collection readonly = new ArrayList ( strs ); //readonly.add( "some string" ); Not allowed Collection copy = new ArrayList (strs); String[] strAry = strs.toArray( new String[] {} ); }

Auto-boxing/unboxing List ints = new ArrayList (); ints.add( 1 ); // auto-boxing to new Integer( 1 ) ints.add( 2 ); ints.add( 3 ); for ( int num : ints ) // Auto-unboxing using obj.intValue() System.out.println( num );

Enhanced for loop (foreach) Improves readability Reduces boilerplate code Works with both arrays and objects that expose an iterator

Enumerated Types Collects related constants into own namespace Codifies the type-safe enum idiom Can have constructors, fields and methods Can subclass Works well with switch statements Adds enum keyword to language. May require source changes before compiling with 1.5 compiler. Existing classes should still work.

Enum example enum Suit {clubs, diamonds, hearts, spades} enum Rank {deuce, three, four, five, six, seven, eight, nine, ten, jack, queen, king, ace} List deck = new ArrayList (); for (Suit suit : Suit.VALUES) for (Rank rank : Rank.VALUES) deck.add(new Card(suit, rank)); Collections.shuffle(deck);

Switch with Enum Color getColor( Suit suit ) { switch( suit ) { case clubs: case spades: return Color.Black; case diamonds: case hearts: return Color.Red; default: throw new AssertionError( suit + not valid value ); }

Variable Arguments String format( String format, Object... args ); format({2} {1}!, World, Hello); Nicer way to call a method that takes an array of some type Class…means Class[] inside of the method Use the enhanced for loop to iterate

Static Import import static java.lang.math.*; … int themax = max( , ); Reduces typing Alleviates need for implementing a Constant Interface Compiler error if names clash

Meta-data Available at compile-time or runtime with reflection Similar concept to XDoclet tags or.Net are standard

References Java Generics Tutorial J2SE 1.5 in a Nutshell A Conversation with Joshua Bloch What's new in J2SE David Flanagan Web log