Boolean b = ...
if (b) {
// do something if b is true
}
if (b) will unbox b. If b is null, the line will throw a NullPointerException. If you cannot guarantee that b will not be null, use this syntax
Boolean b = ...
if (b != null && b) {
// do something if b is true
}
Clarification of Listing 21.1. A ResultSet is closed automatically when the containing Statement/PreparedStatement is closed. Therefore, if you cannot create a ResultSet inside the bracket (for example, because you need to call a set method on the PreparedStatement, you can create the ResultSet outside the bracket and still do not need to call resultSet.close(). The following code is safe.
try (Connection connection = DriverManager.getConnection(dbUrl);
PreparedStatement pStatement = connection.prepareStatement(SOME_SQL)) {
pStatement.setString(1, ...);
ResultSet resultSet = pStatement.executeQuery()) {
while (resultSet.next()) {
System.out.println(resultSet.getString(2));
}
// no need to call resultSet.close() here
} catch (SQLException e) {
e.printStackTrace();
}
For example, see the implementation of java.sql.Statement in the MySQL JDBC driver here:
https://github.com/mysql/mysql-connector-j/blob/release/5.1/src/com/mysql/jdbc/StatementImpl.java