Top Banner
Java 7 Max Ramos Associate Software Engineer, Orange & Bronze Software Labs Oracle Certified Professional (Java SE 6 Programmer)
24

New syntax elements of java 7

Jan 15, 2015

Download

Technology

Max Ramos, Oracle Certified Professional & Associate Software Engineer of Orange and Bronze Software Labs, discusses the new syntax elements of Java 7.

www.orangeandbronze.com
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: New syntax elements of java 7

Java 7Max RamosAssociate Software Engineer, Orange & Bronze Software LabsOracle Certified Professional (Java SE 6 Programmer)

Page 2: New syntax elements of java 7

Overview

Binary Literals Underscore in Numerical Literals String in switch statement Diamond operator Multi-catch Try-with-resources

Page 3: New syntax elements of java 7

Binary Literals

Page 4: New syntax elements of java 7

Before Java 7

int decimal = 100;

int octal = 010; // decimal 8

int hexadecimal = 0x1f; // decimal 31

Page 5: New syntax elements of java 7

In Java 7

int b = 0b100; // decimal 2

or

int b = 0B100;

Page 6: New syntax elements of java 7

Underscore in numerical literals

Page 7: New syntax elements of java 7

Before Java 7

long largeNumber = 94158725;

long manyZeroes = 100000000;

Page 8: New syntax elements of java 7

In Java 7

long largeNumber = 94_158_725;

long manyZeroes = 100_000_000;

Page 9: New syntax elements of java 7

In Java 7

int i = 1000;

int j = 1_000;

if (I == j) { // true

}

or

If (1000 == 1_000) { // true

}

Page 10: New syntax elements of java 7

String in switch statement

Page 11: New syntax elements of java 7

Before Java 7

if (size.equals(“Short”)) {

// do something

} else if (size.equals(“Tall”)) {

// do something

} else if (size.equals(“Grande”)) {

// do something

} else if (size.equals(“Venti”)) {

// do something

} else {

// do something

}

Page 12: New syntax elements of java 7

In Java 7

switch (size) {

case “Short”: // do something

case “Tall”: // do something

case “Grande”: // do something

case “Venti”: // do something

default: // do something

}

Page 13: New syntax elements of java 7

Diamond operator

Page 14: New syntax elements of java 7

Before Java 7

List<String> list = new ArrayList<String>();

Set<Integer> set = new HashSet<Integer>();

Map<String, Object> map = new HashMap<String, Object>();

Page 15: New syntax elements of java 7

In Java 7

List<String> names = new ArrayList<>();

Set<Integer> count = new HashSet<>();

Map<String, Object> map = new HashMap<>();

Page 16: New syntax elements of java 7

Multi-catch

Page 17: New syntax elements of java 7

Before Java 7

try {

// do something that might throw an exception

} catch (ParseException e) {

// do something

} catch (IOException e) {

// do that same thing again

}

Page 18: New syntax elements of java 7

In Java 7

try {

// do something that might throw an exception

} catch (ParseException | IOException e) {

// do something

}

Page 19: New syntax elements of java 7

Try-with-resources

Page 20: New syntax elements of java 7

Before Java 7

public int methodName(String path) throws IOException {

FileReader fr = new FileReader(path);

try {

return fr.read()

} finally {

if (fr != null)

fr.close();

}

}

Page 21: New syntax elements of java 7

In Java 7

public int methodName(String path) throws IOException {

try (FileReader fr = new FileReader(path)){

return fr.read()

}

}

Page 22: New syntax elements of java 7

In Java 7

public void methodName(String path) throws IOException {

try (

FileReader fr = new FileReader(path);

FileWriter fw = new FileWriter(“out.txt”)

){

int i = fr.read();

fw.write(i);

}

}

Page 23: New syntax elements of java 7

About Orange & Bronze Software Labs

• Filipino software development company specializing in Java technology

• Consulting, outsourcing, and offshore product development services using Open Source technologies, with a specialization in the Spring and Grails frameworks

• Enterprise solutions with Google enterprise products and business intelligence solutions with the Pentaho BI Suite

• Offers Java, Agile and Android training courses

www.orangeandbronze.com

Page 24: New syntax elements of java 7

End