Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
1
Java Advanced
Features
Trenton Computer Festival
March 15, 2014
Michael P. Redlich
@mpredli
about.me/mpredli/
Sunday, Ma...
Who’s Mike?
• BS in CS from
• “Petrochemical Research Organization”
• Ai-Logix, Inc. (now AudioCodes)
• Amateur Computer G...
Objectives (1)
• Java Beans
• Exception Handling
• Generics
• Java Database Connectivity
• Java Collections Framework
3
Su...
Java Beans
4
Sunday, March 16, 14
What are Java Beans?
• A method for developing reusable Java
components
• Also known as POJOs (Plain Old Java Objects)
• E...
Java Beans (1)
• A Java class is considered a bean when it:
• implements interface Serializable
• defines a default constru...
Java Beans (2)
• Getter/Setter methods:
• return (get) and assign (set) a bean’s data
members
• Specified naming convention...
8
// PersonBean class (partial listing)
public class PersonBean implements Serializable {
private static final long serial...
Exception Handling
9
Sunday, March 16, 14
What is Exception
Handling?
• A more robust method for handling errors
than fastidiously checking for error codes
• error ...
Exception Handling (1)
• Throw Expression:
• raises the exception
• Try Block:
• contains a throw expression or a method t...
Exception Handling (2)
• Catch Clause(s):
• handles the exception
• defined immediately after the try block
• Finally Claus...
Java Exception Model
(1)
• Checked Exceptions
• enforced by the compiler
• Unchecked Exceptions
• recommended, but not enf...
Java Exception Model
(2)
• Exception Specification
• specify what type of exception(s) a method will
throw
• Termination vs...
15
// ExceptionDemo class
public class ExceptionDemo {
public static void main(String[] args) {
try {
initialize();
}
catc...
Generics
16
Sunday, March 16, 14
What are Generics?
• A mechanism to ensure type safety in Java
collections
• introduced in Java 5
• Similar concept to C++...
Generics (1)
• Prototype:
• visibilityModifier class |
interface name<Type> {}
18
Sunday, March 16, 14
19
// Iterator demo *without* Generics...
List list = new ArrayList();
for(int i = 0;i < 10;++i) {
list.add(new Integer(i)...
20
// Iterator demo *with* Generics...
List<Integer> list = new ArrayList<Integer>();
for(int i = 0;i < 10;++i) {
list.add...
21
// Defining Simple Generics
public interface List<E> {
add(E x);
}
public interface Iterator<E> {
E next();
boolean has...
Java Database
Connectivity (JDBC)
22
Sunday, March 16, 14
What is JDBC?
• A built-in API to access data sources
• relational databases
• spreadsheets
• flat files
• The JDK includes ...
Java Database
Connectivity (1)
• Install database driver and/or ODBC driver
• Establish a connection to the database:
• Cl...
Java Database
Connectivity (2)
• Create JDBC statement:
•Statement statement =
connection.createStatement();
• Obtain resu...
26
// JDBC example
import java.sql.*;
public class DatabaseDemo {
public static void main(String[] args) {
String sql = “S...
Java Collections
Framework
27
Sunday, March 16, 14
What are Java
Collections? (1)
• A single object that groups together
multiple elements
• Collections are used to:
• store...
What is the Java
Collection Framework?
• A unified architecture for collections
• All collection frameworks contain:
• inte...
What is a Collection?
• A single object that groups together
multiple elements
• sometimes referred to as a container
• Co...
Collections (1)
• Implement the Collection interface
• Built-in implementations:
• List
• Set
31
Sunday, March 16, 14
Collections (2)
• Lists
• ordered sequences that support direct
indexing and bi-directional traversal
• Sets
• an unordere...
33
// the Collection interface
public interface Collection<E> extends Iterable<E>{
boolean add(E e);
boolean addAll(Collec...
Iterators
• Used to access elements within an ordered
sequence
• All collections support iterators
• Traversal depends on ...
35
// Iterator demo
import java.util.*;
List<Integer> list = new ArrayList<Integer>();
for(int i = 0;i < 9;++i) {
list.add...
Live Demo!
36
Sunday, March 16, 14
Java IDEs (1)
• IntelliJ
• jetbrains.com/idea
• Eclipse
• eclipse.org
37
Sunday, March 16, 14
Java IDEs (2)
• NetBeans
• netbeans.org
• JBuilder
• embarcadero.com/products/
jbuilder
38
Sunday, March 16, 14
Local Java User Groups
(1)
• ACGNJ Java Users Group
• facilitated by Mike Redlich
• javasig.org
• Princeton Java Users Gro...
Local Java User Groups
(2)
• NewYork Java SIG
• facilitated by Frank Greco
• javasig.com
• Capital District Java Developer...
Further Reading
41
Sunday, March 16, 14
Upcoming Events (1)
• Trenton Computer Festival
• March 14-15, 2014
• tcf-nj.org
• Emerging Technologies for the Enterpris...
43
Upcoming Events (2)
Sunday, March 16, 14
44
Thanks!
mike@redlich.net
@mpredli
javasig.org
Sunday, March 16, 14
Upcoming SlideShare
Loading in …5
×

Java Advanced Features (TCF 2014)

627 views

Published on

Advanced features of Java are reviewed:
* Java Beans
* Exception Handling
* Generics
* JDBC
* Java Collections

Published in: Technology
  • Be the first to comment

Java Advanced Features (TCF 2014)

  1. 1. 1 Java Advanced Features Trenton Computer Festival March 15, 2014 Michael P. Redlich @mpredli about.me/mpredli/ Sunday, March 16, 14
  2. 2. Who’s Mike? • BS in CS from • “Petrochemical Research Organization” • Ai-Logix, Inc. (now AudioCodes) • Amateur Computer Group of New Jersey • Publications • Presentations 2 Sunday, March 16, 14
  3. 3. Objectives (1) • Java Beans • Exception Handling • Generics • Java Database Connectivity • Java Collections Framework 3 Sunday, March 16, 14
  4. 4. Java Beans 4 Sunday, March 16, 14
  5. 5. What are Java Beans? • A method for developing reusable Java components • Also known as POJOs (Plain Old Java Objects) • Easily store and retrieve information 5 Sunday, March 16, 14
  6. 6. Java Beans (1) • A Java class is considered a bean when it: • implements interface Serializable • defines a default constructor • defines properly named getter/setter methods 6 Sunday, March 16, 14
  7. 7. Java Beans (2) • Getter/Setter methods: • return (get) and assign (set) a bean’s data members • Specified naming convention: •getMember •setMember •isValid 7 Sunday, March 16, 14
  8. 8. 8 // PersonBean class (partial listing) public class PersonBean implements Serializable { private static final long serialVersionUID = 7526472295622776147L; private String lastName; private String firstName; private boolean valid; public PersonBean() { } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } // getter/setter for firstName public boolean isValid() { return valid; } } Sunday, March 16, 14
  9. 9. Exception Handling 9 Sunday, March 16, 14
  10. 10. What is Exception Handling? • A more robust method for handling errors than fastidiously checking for error codes • error code checking is tedious and can obscure program logic 10 Sunday, March 16, 14
  11. 11. Exception Handling (1) • Throw Expression: • raises the exception • Try Block: • contains a throw expression or a method that throws an exception 11 Sunday, March 16, 14
  12. 12. Exception Handling (2) • Catch Clause(s): • handles the exception • defined immediately after the try block • Finally Clause: • always gets called regardless of where exception is caught • sets something back to its original state 12 Sunday, March 16, 14
  13. 13. Java Exception Model (1) • Checked Exceptions • enforced by the compiler • Unchecked Exceptions • recommended, but not enforced by the compiler 13 Sunday, March 16, 14
  14. 14. Java Exception Model (2) • Exception Specification • specify what type of exception(s) a method will throw • Termination vs. Resumption semantics 14 Sunday, March 16, 14
  15. 15. 15 // ExceptionDemo class public class ExceptionDemo { public static void main(String[] args) { try { initialize(); } catch(Exception exception) { exception.printStackTrace(); } public void initialize() throws Exception { // contains code that may throw an exception of type Exception } } Sunday, March 16, 14
  16. 16. Generics 16 Sunday, March 16, 14
  17. 17. What are Generics? • A mechanism to ensure type safety in Java collections • introduced in Java 5 • Similar concept to C++ Template mechanism 17 Sunday, March 16, 14
  18. 18. Generics (1) • Prototype: • visibilityModifier class | interface name<Type> {} 18 Sunday, March 16, 14
  19. 19. 19 // Iterator demo *without* Generics... List list = new ArrayList(); for(int i = 0;i < 10;++i) { list.add(new Integer(i)); } Iterator iterator = list.iterator(); while(iterator.hasNext()) { System.out.println(“i = ” + (Integer)iterator.next()); } Sunday, March 16, 14
  20. 20. 20 // Iterator demo *with* Generics... List<Integer> list = new ArrayList<Integer>(); for(int i = 0;i < 10;++i) { list.add(new Integer(i)); } Iterator<Integer> iterator = list.iterator(); while(iterator.hasNext()) { System.out.println(“i = ” + iterator.next()); } Sunday, March 16, 14
  21. 21. 21 // Defining Simple Generics public interface List<E> { add(E x); } public interface Iterator<E> { E next(); boolean hasNext(); } Sunday, March 16, 14
  22. 22. Java Database Connectivity (JDBC) 22 Sunday, March 16, 14
  23. 23. What is JDBC? • A built-in API to access data sources • relational databases • spreadsheets • flat files • The JDK includes a JDBC-ODBC bridge for use with ODBC data sources • type 1 driver 23 Sunday, March 16, 14
  24. 24. Java Database Connectivity (1) • Install database driver and/or ODBC driver • Establish a connection to the database: • Class.forName(driverName); • Connection connection = DriverManager.getConnection(); 24 Sunday, March 16, 14
  25. 25. Java Database Connectivity (2) • Create JDBC statement: •Statement statement = connection.createStatement(); • Obtain result set: • Result result = statement.execute(); • Result result = statement.executeQuery(); 25 Sunday, March 16, 14
  26. 26. 26 // JDBC example import java.sql.*; public class DatabaseDemo { public static void main(String[] args) { String sql = “SELECT * FROM timeZones”; Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”); Connection connection = DriverManager.getConnection(“jdbc:odbc:timezones”,””,””); Statement statement = connection.createStatement(); ResultSet result = statement.executeQuery(sql); while(result.next()) { System.out.println(result.getDouble(2) + “ “ + result.getDouble(3)); } connection.close(); } } Sunday, March 16, 14
  27. 27. Java Collections Framework 27 Sunday, March 16, 14
  28. 28. What are Java Collections? (1) • A single object that groups together multiple elements • Collections are used to: • store • retrieve • manipulate 28 Sunday, March 16, 14
  29. 29. What is the Java Collection Framework? • A unified architecture for collections • All collection frameworks contain: • interfaces • implementations • algorithms • Inspired by the C++ Standard Template Library 29 Sunday, March 16, 14
  30. 30. What is a Collection? • A single object that groups together multiple elements • sometimes referred to as a container • Containers before Java 2 were a disappointment: • only four containers • no built-in algorithms 30 Sunday, March 16, 14
  31. 31. Collections (1) • Implement the Collection interface • Built-in implementations: • List • Set 31 Sunday, March 16, 14
  32. 32. Collections (2) • Lists • ordered sequences that support direct indexing and bi-directional traversal • Sets • an unordered receptacle for elements that conform to the notion of mathematical set 32 Sunday, March 16, 14
  33. 33. 33 // the Collection interface public interface Collection<E> extends Iterable<E>{ boolean add(E e); boolean addAll(Collection<? extends E> collection); void clear(); boolean contains(Object object); boolean containsAll(Collection<?> collection); boolean equals(Object object); int hashCode(); boolean isEmpty(); Iterator<E> iterator(); boolean remove(Object object); boolean removeAll(Collection<?> collection); boolean retainAll(Collection<?> collection); int size(); Object[] toArray(); <T> T[] toArray(T[] array); } Sunday, March 16, 14
  34. 34. Iterators • Used to access elements within an ordered sequence • All collections support iterators • Traversal depends on the collection • All iterators are fail-fast • if the collection is changed by something other than the iterator, the iterator becomes invalid 34 Sunday, March 16, 14
  35. 35. 35 // Iterator demo import java.util.*; List<Integer> list = new ArrayList<Integer>(); for(int i = 0;i < 9;++i) { list.add(new Integer(i)); } Iterator iterator = list.iterator(); while(iterator.hasNext()) { System.out.println(iterator.next()); } 0 1 2 3 4 5 6 7 8 current last Sunday, March 16, 14
  36. 36. Live Demo! 36 Sunday, March 16, 14
  37. 37. Java IDEs (1) • IntelliJ • jetbrains.com/idea • Eclipse • eclipse.org 37 Sunday, March 16, 14
  38. 38. Java IDEs (2) • NetBeans • netbeans.org • JBuilder • embarcadero.com/products/ jbuilder 38 Sunday, March 16, 14
  39. 39. Local Java User Groups (1) • ACGNJ Java Users Group • facilitated by Mike Redlich • javasig.org • Princeton Java Users Group • facilitated byYakov Fain • meetup.com/NJFlex 39 Sunday, March 16, 14
  40. 40. Local Java User Groups (2) • NewYork Java SIG • facilitated by Frank Greco • javasig.com • Capital District Java Developers Network • facilitated by Dan Patsey • cdjdn.com 40 Sunday, March 16, 14
  41. 41. Further Reading 41 Sunday, March 16, 14
  42. 42. Upcoming Events (1) • Trenton Computer Festival • March 14-15, 2014 • tcf-nj.org • Emerging Technologies for the Enterprise • April 22-23, 2014 • phillyemergingtech.com 42 Sunday, March 16, 14
  43. 43. 43 Upcoming Events (2) Sunday, March 16, 14
  44. 44. 44 Thanks! mike@redlich.net @mpredli javasig.org Sunday, March 16, 14

×