⑥ 泛型 generic (⭐️⭐️⭐️⭐️⭐️)
譬如写一个sort method 对integer array,String
array,等其他支持compare的类型数组 使用 generic method,generic class
they allow a type or method to operate an objects of
various types while providing compile-time type safety.
使得 types(classes, interfaces) to be parameters when
defining classes, interfaces, methods - The difference is
that the inputs to formal parameters are values, while
the inputs to type parameters are types.
好处是:
1 elimination of casts
List list = new ArrayList();
list.add("hello");
String s = (String) list.get(0);
When re-written to use generics, the code does not
require casting:
List list = new ArrayList();
list.add("hello");
String s = list.get(0); // no cast
2 stronger type checks at compile time
3 enabling programmers to implement generic algorithms
* [+] Generic Methods
* [+] Generic Classes
⑦ 注解 (⭐️⭐️⭐️⭐️)
form of syntactic metadata that can be added to Java
source code. 如类,方法,变量,参数,包都可以被注解
where annotations can be used
* [+] how to apply annotations
what predefined annotation types are avaiable
how type annotations can be used in xxx
练习:
https://docs.oracle.com/javase/tutorial/java/annotations/Q
andE/answers.html
⑧ 反射 (⭐️⭐️⭐️⭐️)
API used to manipulate classes and everything in class
including fields, methods, constructors, private data etc.
JUnit 4, for example, will use reflection to look through
your classes for methods tagged with the @Test
annotation, and will then call them when running the unit
test.
元编程,debugger,ide,test 等
Method method = foo.getClass().getMethod("doSomething",
null); method.invoke(foo, null);
JDK6:
① Desktop和SystemTray类
② Console类 (⭐️⭐️)
③ Compiler API
④ Web Service
⑤ 公共注解类 (⭐️)
JDK7:
① 字面量改进
② try resource语法 (⭐️⭐️⭐️)
就是类似于Python的with语法
when the try block finishes the FileInputStream will be
closed automatically.
private static void printFileJava7() throws IOException {
try(FileInputStream input = new
FileInputStream("file.txt")) {
int data = input.read();
while(data != -1){
System.out.print((char) data);
data = input.read();
}
}
}
使用custom auto close
public class MyAutoClosable implements AutoCloseable {
public void doIt() {
System.out.println("MyAutoClosable doing
it!");
}
@Override
public void close() throws Exception {
System.out.println("MyAutoClosable closed!");
}
}
private static void myAutoClosable() throws Exception
{
try(MyAutoClosable myAutoClosable = new
MyAutoClosable()){
myAutoClosable.doIt();
}
}
③ Stream API (⭐️⭐️⭐️⭐️)
http://winterbe.com/posts/2014/07/31/java8-stream-tutorial
-examples/
不是InputStream之流而是Monads,对引入functional programming 意义重大
how to work with Java 8 streams
List myList =
Arrays.asList("a1", "a2", "b1", "c2", "c1");
myList
.stream()
.filter(s -> s.startsWith("c"))
.map(String::toUpperCase)
.sorted()
.forEach(System.out::println);
how to use the different kind of available stream
operations
created from various data sources 如collections,lists
and sets
Stream.of("a1", "a2", "a3")
processing order and it affects runtime performance
two intermediate operations map and filter and the
terminal operation forEach
stream operations: reduce, collect, flatMap
parallel stream
capable of operating on multiple threads
这里提供一份简要的JDK5.0 到 JDK 8的崭新并且重要的特性一览和部分解释。因为Java最重要的还是学习基础语法,学会基础语法的基础上,可以搜索学习各自新版本新语法。
JDK5:
⑥ 泛型 generic (⭐️⭐️⭐️⭐️⭐️) 譬如写一个sort method 对integer array,String array,等其他支持compare的类型数组 使用 generic method,generic class they allow a type or method to operate an objects of various types while providing compile-time type safety. 使得 types(classes, interfaces) to be parameters when defining classes, interfaces, methods - The difference is that the inputs to formal parameters are values, while the inputs to type parameters are types. 好处是: 1 elimination of casts List list = new ArrayList(); list.add("hello"); String s = (String) list.get(0); When re-written to use generics, the code does not require casting: List list = new ArrayList();
list.add("hello");
String s = list.get(0); // no cast
JDK6:
JDK7:
② try resource语法 (⭐️⭐️⭐️) 就是类似于Python的with语法 when the try block finishes the FileInputStream will be closed automatically. private static void printFileJava7() throws IOException {
JDK8:
② Lambda表达式 (⭐️⭐️⭐️⭐️⭐️) * [+] lambda expressions functional interfaces method reference for(Object o:objCollection) { someInfra.output(o); }
③ Stream API (⭐️⭐️⭐️⭐️) http://winterbe.com/posts/2014/07/31/java8-stream-tutorial -examples/ 不是InputStream之流而是Monads,对引入functional programming 意义重大 how to work with Java 8 streams List myList =
Arrays.asList("a1", "a2", "b1", "c2", "c1");