这些Java教程是针对JDK 8编写的。本页描述的示例和实践不利用后续版本中引入的改进,并可能使用不再可用的技术。
请参阅Java语言变化以了解Java SE 9及后续版本中更新的语言特性的摘要。
请参阅JDK发行说明以获取有关所有JDK发行版的新功能、增强功能和已删除或已弃用选项的信息。
public final class Algorithm { public static <T> T max(T x, T y) { return x > y ? x : y; } }
public class Pair<K, V> { public Pair(K key, V value) { this.key = key; this.value = value; } public K getKey() { return key; } public V getValue() { return value; } public void setKey(K key) { this.key = key; } public void setValue(V value) { this.value = value; } private K key; private V value; }
public static <T extends Comparable<T>> int findFirstGreaterThan(T[] at, T elem) { // ... }
public static void print(List<? extends Number> list) { for (Number n : list) System.out.print(n + " "); System.out.println(); }
public class Singleton<T> { public static T getInstance() { if (instance == null) instance = new Singleton<T>(); return instance; } private static T instance = null; }
class Shape { /* ... */ } class Circle extends Shape { /* ... */ } class Rectangle extends Shape { /* ... */ } class Node<T> { /* ... */ }
Node<Circle> nc = new Node<>(); Node<Shape> ns = nc;
class Node<T> implements Comparable<T> { public int compareTo(T obj) { /* ... */ } // ... }
Node<String> node = new Node<>(); Comparable<String> comp = node;
public static <T> int findFirst(List<T> list, int begin, int end, UnaryPredicate<T> p)