Docsity

Java 5 New Features: Autoboxing, Generics, Varargs, Formatted I/O, and Enumerations - Prof, Study notes of Programming Languages

Partial preview of the text

Download Java 5 New Features: Autoboxing, Generics, Varargs, Formatted I/O, and Enumerations - Prof and more Study notes Programming Languages in PDF only on Docsity!

JAVA 5

• Material taken from :

Herbert Schildt. Java 2 v5.0 Tiger. McGraw Hill, 2004.

NEW FEATURES FOR JAVA 1.

•^

New naming scheme: Java 5

(Tiger)

•^

New features 1.^

Auto-box and auto-unbox

Generics

for each

statement

Varargs

Formatted I/O

Enumerations

Static import

Metadata

API changes

EXAMPLE

  • Autobox and autounbox mean that

arithmetic expressions are automaticallycoerced^ Integer iX = 123;int i = iX;

EXAMPLE

•^

Auto boxing/unboxing also occurs on methodcall/return^ Class foo {

static int bar(Integer i) { return i; }public static void main(String argv[]){

Integer iX = bar(123);System.out.println(iX); } }

WARNING

  • Expression evaluation can lead to a

cascade of expensive boxing/unboxingactions^ int i;Integer iX = 2;iX = (iX + iX++) / 2;

2. GENERICS

  • Parameterized declarations
    • Methods, classes and interfaces
      • Homogeneous collections without explicit

downcasting

  • Java Collections framework• Type safety and cleaner code

EXAMPLE

class Foo {

T x;

// instance variable

Foo(T y) { x = y; }

//constructor

T getFoo() { return x; }

// getter

String whoAmI() {

// reflection

return( x.getClass().getName()); } }

EXAMPLE USE

Foo dbl;dbl = new Foo(42.); // boxdouble d = dbl.getFoo();

// unbox

dbl.whoAmI();

// Java.lang.Double

©2007 by Spencer Rugaber

WILDCARDS

-^

Denoted by

?

-^

Normally, references to a generic formal parameter (

T

)

within a generic stand for the same actual argumentspecified at generic instantiation time

-^

It is sometimes useful to allow a reference to any legalactual argument and not just

T

class foo {

int bar(foo<?> x) { ...}

//? instead of T

} foo iX;iX.bar(foo);

-^

The use of wildcards does not modify boundedness of

T

,

but wildcard themselves may be bounded

-^

You can even bound them from below^ <? super

classname

GENERIC METHODS

• A method may be generic even if its

class is not

• Actual arguments of calls need not

appear in angle brackets^ – They are inferred by compiler– Similar to Ada

• Same for constructors

GENERIC INTERFACES

• Interfaces can also be generic• Implementations of generic interfaces

must be themselves generic^ – Implementations of generic interface

instantiations need not be

• Parameters may be bounded

RAW TYPES

• Uses of a generic without a type

parameter are treated as if theparameter were

Object

  • Done for backwards compatibility– Not type safe; compiler produces

warnings

HOMOGENEOUS COLLECTIONS • Java collection classes have been

reengineered as generics with aparameter to indicate the type of thecollected element^ – Automatic enforcement on addition to a

collection

  • Downcasting not explicitly required on

extraction

  • Iterators have likewise been made generic

LIMITATIONS ON GENERICS

  • Can’t create instances of a type

T

with

new

  • static

members can’t use type

parameters

  • Can’t instantiate array whose base

element type is specified by a typeparameter

  • No generic

Exception

classes