归纳一些值得在面试时回答的Java新特性的点
JDK 7
http://www.oracle.com/technetwork/java/javase/jdk7-relnotes-418459.html
Strings in switch Statements
In the JDK 7 release, you can use a String object in the expression of a switch statement:
|
|
The switch statement compares the String object in its expression with the expressions associated with each case label as if it were using the String.equals method; consequently, the comparison of String objects in switch statements is case sensitive. The Java compiler generates generally more efficient bytecode from switch statements that use String objects than from chained if-then-else statements.
Type Inference for Generic Instance Creation
You can replace the type arguments required to invoke the constructor of a generic class with an empty set of type parameters (<>) as long as the compiler can infer the type arguments from the context. This pair of angle brackets is informally called the diamond.
For example, consider the following variable declaration:
|
|
In Java SE 7, you can substitute the parameterized type of the constructor with an empty set of type parameters (<>):
|
|
Binary Literals
In Java SE 7, the integral types (byte, short, int, and long) can also be expressed using the binary number system. To specify a binary literal, add the prefix 0b or 0B to the number. The following examples show binary literals:
|
|
Underscores in Numeric Literals
In Java SE 7 and later, any number of underscore characters (_) can appear anywhere between digits in a numerical literal. This feature enables you, for example, to separate groups of digits in numeric literals, which can improve the readability of your code.
For instance, if your code contains numbers with many digits, you can use an underscore character to separate digits in groups of three, similar to how you would use a punctuation mark like a comma, or a space, as a separator.
The following example shows other ways you can use the underscore in numeric literals:
|
|
Garbage-First Collector
The Garbage-First (G1) garbage collector is fully supported in Oracle JDK 7 update 4 and later releases. The G1 collector is a server-style garbage collector, targeted for multi-processor machines with large memories. It meets garbage collection (GC) pause time goals with high probability, while achieving high throughput. Whole-heap operations, such as global marking, are performed concurrently with the application threads. This prevents interruptions proportional to heap or live-data size.
JDK 8
http://www.oracle.com/technetwork/java/javase/8-whats-new-2157071.html
Lambda Expressions
Lambda Expressions, a new language feature, has been introduced in this release. They enable you to treat functionality as a method argument, or code as data. Lambda expressions let you express instances of single-method interfaces (referred to as functional interfaces) more compactly.
Default Methods
|
|
Suppose that you want to add new functionality to the TimeClient interface, such as the ability to specify a time zone through a ZonedDateTime object (which is like a LocalDateTime object except that it stores time zone information).
Following this modification to the TimeClient interface, you would also have to modify the class SimpleTimeClient and implement the method getZonedDateTime. However, rather than leaving getZonedDateTime as abstract (as in the previous example), you can instead define a default implementation. (Remember that an abstract method is a method declared without an implementation.)
Java Date-Time Packages
java.time- Classes for date, time, date and time combined, time zones, instants, duration, and clocks.
补充 HashMap & ConcurrentHashMap
数组 + 链表 -> Node数组+链表+红黑树(TreeNode)
链表长度超过8时,链表转换为红黑树
锁分段 -> CAS算法 + synchronized(put操作hash值相同节点)
CAS三个操作数:内存值,期待值,新值。如果内存值和期待值相匹配,更新为新值;否则不执行操作。
补充 移除永久代
引入元空间Metaspace
总结
JDK 7
- switch支持String类型
- 泛型类型指定简化
- 二进制表示
- 数字中下划线
- G1垃圾收集器
JDK 8
- Lambda表达式
- 接口中默认方法
- 新的时间类



