int value=values.get(0); JAVA 5 value=primitive; BOXING primitive=value ; UNBOXING Automatic operation With generics the type can’t be a primitive, but is not a problem Integer value; int primitive; o Enumerations: Special data type that enables for a variable to be a set of predefined constants
0; i < values.size(); i++) { result += values.get(i) + "-"; } String result = ""; for (final String value : values) { result += value + "-"; } OLDER JAVA JAVA 5 o Foreach o Static imports double r = Math.cos(PI * theta); OLDER JAVA JAVA 5 double r = Math.cos(Math.PI * theta);
int sumTwo(final int a, final int b) { return a + b; } public int sumThree(final int a, final int b, final int c) { return a + b + c; } sum(2, 2)); //two numbers sum(2, 2, 2)); //three numbers public int sum(final int... numbers) { int total = 0; for (final int number : numbers) { total += number; } return total; } OLDER JAVA JAVA 5 o Varargs Java 5
o JVM support for dynamic languages o Binary integer literals/underscores o Varargs simplified int million = 1_000_000 int telephone = 983_71_25_03 int num = 0b101
int a = Integer.parseInt(num1); int b = Integer.parseInt(num2); result = a / b; } catch (NumberFormatException ex1) { System.out.println("invalid number"); } catch (ArithmeticException ex2) { System.out.println("zero"); } catch (Exception e) { e.printStackTrace(); } return result; } public double divide(){ double result = 0d; try { int a = Integer.parseInt(num1); int b = Integer.parseInt(num2); result = a / b; } catch (NumberFormatException|ArithmeticException ex){ System.out.println("invalid number or zero"); } catch (Exception e) { e.printStackTrace(); } return result; } OLDER JAVA MULTI CATCH - JAVA 7 o Catch multiple exceptions
Metaspace OutOfMemory errors disappear? -> not so fast o New methods in Collections (almost all lambda- related) o Small changes everywhere (concurrency, generics, String, File, Math)
& eclipse with Java 8 support (+plugin) • Slides that inspired this talk • Quick survey of Java 7 features • Quick survey of Java 8 features • New Date and Time API • Good presentation of Java 8 • There is even a book (wait, Java 8 isn’t live yet!)