public class JDK7GenericTest {
public static void main(String[] args) {
// Pre-JDK 7
List<String> lst1 = new ArrayList<String>();
// JDK 7 supports limited type inference for generic instance creation
List<String> lst2 = new ArrayList<>();
lst1.add("Mon");
lst1.add("Tue");
lst2.add("Wed");
lst2.add("Thu");
for (String item: lst1) {
System.out.println(item);
}
for (String item: lst2) {
System.out.println(item);
}
}
}
/**
* A resource that must be closed when it is no longer needed.
*
* @author Josh Bloch
* @since 1.7
*/
public interface AutoCloseable {
/**
* Closes this resource, relinquishing any underlying resources.
* This method is invoked automatically on objects managed by the
* {@code try}-with-resources statement.
*
*/
void close() throws Exception;
}
class TryClose implements AutoCloseable {
@Override
public void close() throw Exception {
System.out.println(" Custom close method …
close resources ");
}
}
//请看jdk自带类BufferedReader如何实现close方法(当然还有很多类似类型的类)
public void close() throws IOException {
synchronized (lock) {
if (in == null)
return;
in.close();
in = null;
cb = null;
}
}