Java Magazine, Jan/Feb 2017
ORACLE COM JAVAMAGAZINE JANUARY FEBRUARY 2017 67 fix this different orderings for a class This is important because I might for example want a list of students in grade order or if Im the basketball coach in height order Similarly if Im the accountant I might want students in order of the amount of money each owes the school Its not possible to have multiple orders and expect them all to be consistent with equals However thats generally still considered to be a good thing if its possible as the documentation for Comparator notes Caution should be exercised when using a comparator capable of imposing an ordering inconsistent with equals to order a sorted set or sorted map Mostly however its important to be aware of the wrinkles that arise when order and equality are inconsistent Question 3 The correct answer is option A Proper release of nonmemory resources is often crucial to the correct longterm running of any server type program Database connections are certainly important resources requiring proper treatment and in Java 7 the JDBC API was extended to make good use of the try with resources feature In particular Connection Statement and ResultSet all implement AutoCloseable Its probably tempting to guess that option D which doesnt use try with resources is incorrect but you cant reject it based on not liking it Closer inspection however shows that this option does not close the main database connection Theres no mandate that absolutely requires that a connection be used once and then released but the question specifically asks which option releases all the resources Because of this option D can properly be rejected because it leaves the connection open The other three options all use the try with resources mechanism but auto close diferent resources Option A closes the Connection and the Statement but not the ResultSet Option B closes the Statement and the ResultSet but not the Connection Option C closes the ResultSet alone Is it possible that none of these works fully correctly according to the requirements of the question Well it turns out that the specification of these behaviors helps out If you look at the API documentation for the Connection close method you can see that it says the method releases this Connection objects database and JDBC resources immediately This suggests that if you auto close the Connection alone you have fulfilled the requirements of the question However theres no such option Option A does however close both the Connection and the Statement These will close in reverse order so the Statement is closed first and then the Connection Regardless the efect is that all resources will be closed In fact theres a comment about the Statement close method that mirrors the one for the Connection close method Releases this Statement objects database and JDBC resources immediately It is generally good practice to release resources as soon as you are finished with them to avoid tying up database resources This tells you that option A achieves the desired efect even though it does not explicitly close the ResultSet The auto closing of subresources can be significant in this type of coding Imagine that youve prepared a result set containing the data you need and then you decide to be a good citizen and release the unneeded Statement and Connection This unfortunately would close your ResultSet too taking your data away potentially before you have finished using it article Simon Roberts joined Sun Microsystems in time to teach Suns first Java classes in the UK He created the Sun Certified Java Programmer and Sun Certified Java Developer exams He wrote several Java certification guides and is currently a freelance educator who teaches at many large companies in Silicon Valley and around the world He remains involved with Oracles Java certification projects
You must have JavaScript enabled to view digital editions.