java15

27
Java 1.5 New Features

Upload: hariprasanna-v

Post on 15-May-2015

1.523 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Java15

Java 1.5 New Features

Page 2: Java15

java.applet, java.awt, java.io, java.lang, java.net, java.util

java.math, java.rmi, java.security, java.sql, java.text, java.beans

javax.accessibility, javax.swing, org.omg

javax.naming, javax.sound, javax.transaction

java.nio, javax.imageio,javax.net, javax.print,javax.security, org.w3c

javax.activity,javax.management

Java 1.08 packages212 classes

Java 1.123 packages504 classes

Java 1.259 packages1520 classes

Java 1.377 packages1595 classes

Java 1.4103 packages2175 classes

Java 1.5131 packages2656 classes

New Events

Inner class

Object Serialization

Jar Files

International

Reflection

JDBC

RMI

JFC/Swing

Drag and Drop

Java2D

CORBA

JNDI

Java Sound

Timer

Regular ExpLoggingAssertionsNIO

Page 3: Java15

New Features

• Scanner

• Printf

• Autoboxing

• Enumerated Types

• Foreach loop

• Generic Types

Page 4: Java15

Other New Features

• Static import: import static java.Math.*;

• Variable length argument lists.

• Semaphores

• Metadata

Page 5: Java15

java.util.Scanner

Scanner stdin = Scanner.create(System.in);

int n = stdin.nextInt();

String s = stdin.next();

boolean b = stdin.hasNextInt()

Page 6: Java15

printf method

• Similar to C style printf

• Handles – strings– native types – numeric types– java.util.Date

• See java.util.Formatter for details

Page 7: Java15

Autoboxing/Unboxing

• Wrap ints into Integers

• Extract ints from Integers

This is now legal:

Integer x = 6; //6 is boxed

Integer y = 2*x + 3; //x is unboxed, 15 is boxed

You could hide primitives entirely!

Page 8: Java15

Object Only Performance

int x = 6;

int y = 2*x;

Code:

0: bipush 6

2: istore_1

3: iconst_2

4: iload_1

5: imul

6: istore_2

Page 9: Java15

Object Only PerformanceInteger x = 6;Integer y = 2*x;

Code: 0: bipush 6 2: invokestatic #2;

//Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer; 5: astore_1 6: iconst_2 7: aload_1 8: invokevirtual #3;

//Method java/lang/Integer.intValue:()I 11: imul 12: invokestatic #2;

//Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer; 15: astore_2

Page 10: Java15

Object Only Performance

Integer x = Integer.valueOf(6);

Integer y = Integer.valueOf(2 * x.IntValue);

Generates the same byte code.

Page 11: Java15

Type-safe enum

enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY,

FRIDAY, SATURDAY }

//usedDay today = Day.WEDNESDAY;

switch(today){case SUNDAY:

break;//…

}

Page 12: Java15

More Complex Enum

enum Suit { CLUBS(Color.BLACK), DIAMONDS(Color.RED), HEARTS(Color.RED), SPADES(Color.BLACK);

private Color color;

Suit(Color color) { this.color = color; }

public Color getColor() { return color; } }

Page 13: Java15

GenericsArrayList<Number> list = new

ArrayList<Number>();

list.add(new Integer(5)); list.add(6); list.add(new Double(3.14159/2));list.add(new

BigInteger("123456789012301234567890")); list.add(new Long(127L));

Number n = list.get(i);

Page 14: Java15

Defining Generics

public class GenericStack<T> implements Iterable<T> { private T[] data; …public GenericStack() {

data = (T[])new Object[MIN_SIZE]; …}

public T pop(){ … }public void push(T item) { … }

public Iterator<T> iterator(){ … } //since it is iterable}

Page 15: Java15

Defining Generic Iterator//inner class of GenericStackprivate class GenericStackIterator implements Iterator<T> {

int index; GenericStackTestIterator() {

index = top; }

public boolean hasNext() { return index >= 0; } public T next() {

if(index < 0) { throw new java.util.NoSuchElementException();

} T item = data[index]; index--; return item;

}

public void remove() { throw new UnsupportedOperationException( )

} }

Page 16: Java15

For each loop (arrays)

double[] array = {2.5, 5.2, 7.9, 4.3, 2.0};

for(double d: array) {

System.out.println(d); }

Iterate (forward) through the array without paying attention to indices.

Page 17: Java15

For each loop (Collections)

ArrayList<Integer> list = new ArrayList<Integer>();

list.add(7);

list.add(15);

list.add(-67);

for(Integer number: list){ System.out.println(number);

}

Page 18: Java15

For each loop enums

for( Suit s: Suit.values()){

System.out.println(s.getColor());

}

values() returns an Iterator for enum types

Page 19: Java15

Wish list

• Preprocessor– Embedded languages: SQL,

Regular Expression syntax.

• Operator Overloading– Assignment– Equality– [ ] for Lists

Page 20: Java15

Classroom Incorporation

• Investigate use with undergrad.

• Example: Servlets/JSP in Databases

JavaCard in Architecture.

• Metadata for Generating Code in Compilers

Page 21: Java15

References

• Calvin Austin. J2SE 1.5 in a Nutshell. http://java.sun.com/developer/technicalArticles/releases/j2se15

• Gilad Bracha. Generics in the Java Programming Language. http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf

• David Flanagan. Java in a Nutshell. O’Reilly. 1st, 2nd and 3rd editions.

• Cay Hortsmann and Gary Cornell. Core Java 2. Prentice Hall.

Page 22: Java15

Examples online

http://cs.gettysburg.edu/~cpresser/java

The latest additions will be made next week

Including this presentation.

email: [email protected]

Page 23: Java15

The End….

Page 24: Java15

No, really.Stop clicking.

Page 25: Java15

Java History

– New event model– Inner classes– Object Serialization– JAR files– Internationalization– Reflection– Java Database Connectivity (JDBC)– Remote Method Invocation (RMI)

Java 1.0 8 Packages, 212 Classes

Java 1.1 23 Packages, 504 Classes

Page 26: Java15

Java History

– JFC (Swing)– Drag and Drop– Java 2D

Java 1.2 59 Packages, 1520 Classes

– Java Naming and Directory Interface (JNDI)– Java Sound– Timer class

Java 1.3 77 Packages, 1595 Classes

Page 27: Java15

Java History

– Regular Expressions– Assertions– Logging– New I/O (NIO)

Java 1.4 103 Packages, 2175 Classes

– What, and ruin the surprise?

Java 1.5 131 Packages, 2656 Classes