JAVA 1.5 Fernando Almeida Octubre 2004. Introducción Java Specification Request (JSR) 14Java...

26
JAVA 1.5 Fernando Almeida Octubre 2004

Transcript of JAVA 1.5 Fernando Almeida Octubre 2004. Introducción Java Specification Request (JSR) 14Java...

Page 1: JAVA 1.5 Fernando Almeida Octubre 2004. Introducción Java Specification Request (JSR) 14Java Specification Request (JSR) 14 propone introducir tipos y.

JAVA 1.5

Fernando Almeida

Octubre 2004

Page 2: JAVA 1.5 Fernando Almeida Octubre 2004. Introducción Java Specification Request (JSR) 14Java Specification Request (JSR) 14 propone introducir tipos y.

IntroducciónJava Specification Request (JSR) 14 propone introducir tipos y métodos genéricos en el lenguaje de programación JAVA. Desde los comienzos de JAVA, los desarrolladores han intentado agregarle los tipos genéricos al lenguaje. Los tipos genéricos han sido usados por años en otros lenguajes de programación y ahora serán parte de Java 1.5.

Page 3: JAVA 1.5 Fernando Almeida Octubre 2004. Introducción Java Specification Request (JSR) 14Java Specification Request (JSR) 14 propone introducir tipos y.

Mejoras generales de JAVA 1.5

•Incorporación de Tipos Genéricos (Generics).

•Autoencapsulamiento y Desencapsulamiento.

•Importación estática.

•Sentencia FOR mejorada.

Page 4: JAVA 1.5 Fernando Almeida Octubre 2004. Introducción Java Specification Request (JSR) 14Java Specification Request (JSR) 14 propone introducir tipos y.

Tipos Genéricos

Probablemente sea más conocido por parameterized types (tipos-parametrizados) ó templates (plantillas).

Los dos mayores beneficios de los tipos genéricos in JAVA son:

1- Reducir en número de conversiones (casts) en el programa, así reduciendo el número de

posibles errores.

2- Mejorar la claridad del código.

Page 5: JAVA 1.5 Fernando Almeida Octubre 2004. Introducción Java Specification Request (JSR) 14Java Specification Request (JSR) 14 propone introducir tipos y.

Ejemplo 1 : Reducción de Casts

class DangerousCast { public static void main(String[] args) { Stack stack = new Stack(); stack.push(new Integer(1)); stack.push("2"); stack.push(new Integer(3));

while(!stack.isEmpty()) { Integer integer = (Integer) stack.pop(); . . . . } } }

Page 6: JAVA 1.5 Fernando Almeida Octubre 2004. Introducción Java Specification Request (JSR) 14Java Specification Request (JSR) 14 propone introducir tipos y.

Ejemplo1 : Reducción de Casts(Generics)

class CompilerSavesTheDay {

public static void main(String[] args) { Stack<Integer> stack = Stack<Integer>(); stack.push(new Integer(1)); stack.push("2"); //Error de compilación. stack.push(new Integer(3));  while(!stack.isEmpty()) { Integer integer = stack.pop(); . . . . } } }

Page 7: JAVA 1.5 Fernando Almeida Octubre 2004. Introducción Java Specification Request (JSR) 14Java Specification Request (JSR) 14 propone introducir tipos y.

Mejorar la claridad del código

// Instancia de la estructura de datos List list = new LinkedList(); list.add(new LinkedList()); 

// Agregando un valor ((List) list.get(0)).add("value");

// Obteniendo un valor String value = (String) ((List) list.get(0)).get(0);

Page 8: JAVA 1.5 Fernando Almeida Octubre 2004. Introducción Java Specification Request (JSR) 14Java Specification Request (JSR) 14 propone introducir tipos y.

Mejorar la claridad del código(Generics)

// instancia de la estructura de datosList<List<String>> list = new LinkedList<List<String>>(); list.add(new LinkedList<String>()); 

// Agregando un valor list.get(0).add("value"); 

// Obteniendo un valor  String value = list.get(0).get(0);

Page 9: JAVA 1.5 Fernando Almeida Octubre 2004. Introducción Java Specification Request (JSR) 14Java Specification Request (JSR) 14 propone introducir tipos y.

Ejemplo de colecciones con Generics

// Declaración de Map que acepta Integers y Strings. Map<Integer, String> meses = new HashMap<Integer,String>(); months.put(1, "Enero"); months.put(2, "Febrero"); months.put(3, "Marzo"); .... // retorna "Enero“ String month = meses.get(1);

// Declaración de una Lista de Strings List<String> dias = new ArrayList<String>(); days.add("Domingo"); days.add("Lunes"); days.add("Martes");

Page 10: JAVA 1.5 Fernando Almeida Octubre 2004. Introducción Java Specification Request (JSR) 14Java Specification Request (JSR) 14 propone introducir tipos y.

Ejemplo de colecciones con Generics

// Definición de un Comparator para ordenar descendentemente// Strings en una rutina de sort.Comparator<String> comparator =

new Comparator<String>() {public int compare(String s1, String s2) {

return -s1.compareTo(s2); } };

// Ordenar los días en orden descendente Collections.sort(dias, comparator);

// retorna Martes String dia = dias.get(0);

Page 11: JAVA 1.5 Fernando Almeida Octubre 2004. Introducción Java Specification Request (JSR) 14Java Specification Request (JSR) 14 propone introducir tipos y.

Colección sin utilizar Generics (Advertencia del compilador)

// El siguiente código funciona pero genera una// advertencia de compilación.

List diasNoSeleccionados = new ArrayList();diasNoSeleccionados.add("Domingo");diasNoSeleccionados.add("Lunes");diasNoSeleccionados.add("Martes");String diaNoSeleccionado ;diaNoSeleccionado = (String) diasNoSeleccionados.get(0);

Page 12: JAVA 1.5 Fernando Almeida Octubre 2004. Introducción Java Specification Request (JSR) 14Java Specification Request (JSR) 14 propone introducir tipos y.

public class Par { private Object primero; private Object segundo;

public Par(Object primero, Object segundo) { this.primero = primero; this.segundo = segundo; }

public Object getPrimero() { return this.primero; }

Clases Genéricas

Page 13: JAVA 1.5 Fernando Almeida Octubre 2004. Introducción Java Specification Request (JSR) 14Java Specification Request (JSR) 14 propone introducir tipos y.

Clases Genéricas public Object getSegundo() { return this.segundo; } public static void main(String[] args) {

// Par de número - mes Par ene = new Par(new Integer(1), "Enero"); int numMes ; numMes= ((Integer)ene.getPrimero()).intValue(); String nombreMes = (String) ene.getSegundo(); } }

Page 14: JAVA 1.5 Fernando Almeida Octubre 2004. Introducción Java Specification Request (JSR) 14Java Specification Request (JSR) 14 propone introducir tipos y.

Clases Genéricas (Generics)

public class Par<T1, T2> { private T1 primero; private T2 segundo;

public Pair(T1 primero, T2 segundo) { this.primero = primero; this.segundo = segundo; }

public T1 getPrimero() { return this.primero; }

Page 15: JAVA 1.5 Fernando Almeida Octubre 2004. Introducción Java Specification Request (JSR) 14Java Specification Request (JSR) 14 propone introducir tipos y.

Clases Gnéricas (Generics)

public T2 getSegundo() { return this.segundo; }

public static void main(String[] args) { // Par número – mes Par<Integer, String> ene; ene = new Par<Integer, String>(1, "Enero"); int numMes = ene.getPrimero(); String nombreMes = ene.getSegundo();  } }

Page 16: JAVA 1.5 Fernando Almeida Octubre 2004. Introducción Java Specification Request (JSR) 14Java Specification Request (JSR) 14 propone introducir tipos y.

Implementar una extensión de java.util.ArrayList que solo acepte tipos Number.

public class MyNumberList<T extends Number> extends java.util.ArrayList<T> {

public double sum() { .... }

public double average() { .... } }

Page 17: JAVA 1.5 Fernando Almeida Octubre 2004. Introducción Java Specification Request (JSR) 14Java Specification Request (JSR) 14 propone introducir tipos y.

Continuación MyNumberList

Las siguientes declaraciones son legales:

MyNumberList<Double> myNumberList = new MyNumberList<Double>();

MyNumberList<Integer> myNumberList2 = new MyNumberList<Integer>();

Pero las siguientes declaraciones no lo son:

MyNumberList<String> myStringList = new MyNumberList<String>();

MyNumberList<Boolean> myBooleanList = new MyNumberList<Boolean>();

Page 18: JAVA 1.5 Fernando Almeida Octubre 2004. Introducción Java Specification Request (JSR) 14Java Specification Request (JSR) 14 propone introducir tipos y.

Métodos Genericos

Así como las Clases e interfaces pueden ser genéricas, los métodos también pueden recibir parámetros genéricos.

Veamos un ejemplo para una rutina de ordenamiento que funcionaría para una Collection de Objetos Comparable.

public static <T extends Comparable> void mySortRoutine(Collection<T> collection);

Page 19: JAVA 1.5 Fernando Almeida Octubre 2004. Introducción Java Specification Request (JSR) 14Java Specification Request (JSR) 14 propone introducir tipos y.

Autoboxing / UnboxingList intList = new ArrayList(); for (int i=0; i < 10; i++) { // Ahora debemos encapsular todo int como Integer

intList.add(new Integer(i));

}

int sum = 0;

for (int i=0; i < intList.size(); i++) {

// Precisamos castear a Integer y utilizar intValue() // para obtener un int.

int num = ((Integer) intList.get(i)).intValue(); sum += num;

}

Page 20: JAVA 1.5 Fernando Almeida Octubre 2004. Introducción Java Specification Request (JSR) 14Java Specification Request (JSR) 14 propone introducir tipos y.

Autoboxing / Unboxing (Generics)

List<Integer> intList = new ArrayList<Integer>();

for (int i=0; i < 10; i++) { // No es necesario encapsular los ints intList.add(i); }  int sum = 0; for (int i=0; i < intList.size(); i++) { // Se puede obtener el int directamente sum += intList.get(i); }

Page 21: JAVA 1.5 Fernando Almeida Octubre 2004. Introducción Java Specification Request (JSR) 14Java Specification Request (JSR) 14 propone introducir tipos y.

Importación estática// "Constant Interface" antipattern – No usar!public interface Physics { public static final double AVOGADROS_NUMBER = 6.02214199e23; public static final double BOLTZMANN_CONSTANT = 1.3806503e-23; public static final double ELECTRON_MASS = 9.10938188e-31;}  public class Guacamole implements Physics { public static void main(String[] args) { double moles = ...; double molecules = AVOGADROS_NUMBER * moles; ... }}

Page 22: JAVA 1.5 Fernando Almeida Octubre 2004. Introducción Java Specification Request (JSR) 14Java Specification Request (JSR) 14 propone introducir tipos y.

Importación estáticaLa idea de implementar una interfaz es de proveer un comportamiento y no de proveer constantes. La importación estática es una alternativa bastante clara, es análoga a la importación de paquete, excepto que esta solo importa los miembros estáticos.

import static org.iso.Physics.*; class Guacamole { public static void main(String[] args) { double molecules = AVOGADROS_NUMBER * moles; ... }}

Page 23: JAVA 1.5 Fernando Almeida Octubre 2004. Introducción Java Specification Request (JSR) 14Java Specification Request (JSR) 14 propone introducir tipos y.

Iteración For con Colecciones

void cancelAll(Collection c) { for (Iterator i = c.iterator(); i.hasNext(); ) { TimerTask tt = (TimerTask) i.next(); tt.cancel(); }}

Page 24: JAVA 1.5 Fernando Almeida Octubre 2004. Introducción Java Specification Request (JSR) 14Java Specification Request (JSR) 14 propone introducir tipos y.

Iteración For con Colecciones

void cancelAll(Collection c) { for (Object o : c) ((TimerTask)o).cancel();}

Aún mejor con el uso de tipos genéricos (Generics).

void cancelAll(Collection<TimerTask> c) { for (TimerTask task : c) task.cancel();}

Page 25: JAVA 1.5 Fernando Almeida Octubre 2004. Introducción Java Specification Request (JSR) 14Java Specification Request (JSR) 14 propone introducir tipos y.

Links

http://java.sun.com/j2se/1.5.0/index.jsp

http://java.sun.com/j2se/1.5.0/download.jsp

Page 26: JAVA 1.5 Fernando Almeida Octubre 2004. Introducción Java Specification Request (JSR) 14Java Specification Request (JSR) 14 propone introducir tipos y.

Fin de la presentación de JAVA 1.5