Java SE 8 introduces significant features like lambda expressions, the Stream API, and the Date and Time API, enhancing functional programming capabilities. It enables more efficient utilization of multicore processors and simplifies code through anonymous functions and method references. Additionally, it maintains backward compatibility with default methods in interfaces and improves type inference.
Java SE 8 introduces significant features like lambda expressions, the Stream API, and the Date and Time API, enhancing functional programming capabilities. It enables more efficient utilization of multicore processors and simplifies code through anonymous functions and method references. Additionally, it maintains backward compatibility with default methods in interfaces and improves type inference.
本文档介绍了自定义标签的学习目标和方法,包括使用 tag file 和 simple tag 进行自定义标签的实现。文中详细说明了 tag file 的结构及其在 JSP 中的使用方式,以及标签处理器的生命周期和 API 架构。还涵盖了如何处理标签属性、与父标签沟通以及将 tag file 打包为 JAR 文件的注意事项。
3. Update to date
• JDK 7 Features updated ! Plan B
has apparently been approved
– http://www.baptiste-wicht.com/2010/09/jdk-7-
features-updated-plan-b-is-apparently-here/
• 2011 Java SE 7 沒有 Closure … Orz
• 2012 Java SE 8 才有 Closure ... XD
• 2012 世界末日才有 Closure … 囧rz
5. What is
http://en.wikipedia.org/wiki/Lambda
In programming languages such as
Lisp and Python, lambda is an
operator used to denote
anonymous functions or closures
/lambda/
24. My name is Python ….
def max(m, n):
return m if m > n else n
print(max(10, 3)) # 顯示 10
maximum = max
print(maximum(10, 3)) # 顯示 10
max = lambda m, n: m if m > n else n
print(max(10, 3)) # 顯示 10
25. 函式定義
function max(m, n) { def max(m, n):
if(m > n) { return m if m > n else n
return m;
}
return n;
}
匿名函式
function(m, n) { lambda m, n: m if m > n else n
if(m > n) {
return m;
}
return n;
};
26. import math
從函式傳回函式
def prepare_factor(max):
# 一些建立質數表的過程,需要一些時間與資源
primes = [i for i in range(2, max) if prime[i] == 1] # 質數表
def factor(num):
# 利用質數表進行因式分解的過程
while primes[i] ** 2 <= num:
if num % primes[i] == 0:
Closure list.append(primes[i])
num //= primes[i]
else:
i += 1
return factor # 傳回函式
factor = prepare_factor(1000)
print(factor(100)) # 顯示 [2, 2, 5, 5]
31. def func(): def func():
x = 10 x = 10
def getX(): def getX():
return x return x
def setX(n): def setX(n):
x = n nonlocal x = n
return (getX, setX) return (getX, setX)
getX, setX = func() getX, setX = func()
getX() # 10 getX() # 10
setX(20) setX(20)
getX() # 10 getX() # 20
32. 函式定義
def max(m: Int, n: Int): Int = {
if (m > n)
m
else
n
}
匿名函式
val max: (Int, Int) => Int = (m: Int, n: Int) => if(m > n) m else n
33. 函式定義
def max(m: Int, n: Int): Int = {
if (m > n)
m
else
n
}
匿名函式
val max: (Int, Int) => Int = (m: Int, n: Int) => if(m > n) m else n
34. 動態定型語言
• 型態資訊是在資料本身而不是變數
• 變數本身的型態是在執行時期運算得知,
也同一變數可以參考至各種型態的資料。
function max(m, n) { def max(m, n):
if(m > n) { return m if m > n else n
return m;
}
return n;
}
function(m, n) { max2 = lambda m, n: m if m > n else n
if(m > n) {
return m;
}
return n;
};
35. 靜態定型語言
• 根據資料的型態資訊,將變數及運算式進
行分類,型態資訊是在宣告的變數上
• 在執行時期變數的型態資訊無法改變,資
料只能被指定至同一種型態的變數
def max(m: Int, n: Int): Int = {
if (m > n)
語法上的冗長
m
else
n
}
val max: (Int, Int) => Int = (m: Int, n: Int) => if(m > n) m else n
36. 如果要設計callback函式…
def selection(number: Array[Int], order: (Int, Int) => Boolean) {
...
val o = order(a, b)
…
}
val arr = Array(2, 5, 1, 7, 8)
selection(arr, (a: Int, b: Int) => a > b)
val arr = Array(2, 5, 1, 7, 8)
selection(arr, (a, b) => a > b)
val arr = Array(2, 5, 1, 7, 8) 類型推斷
selection(arr, (_: Int) > (_: Int))
type inference
val arr = Array(2, 5, 1, 7, 8)
selection(arr, _ > _)
40. 編譯器強迫你要加上 final
把外部res指定
public void doSome() {
final int res = 10;
ISome o = new ISome() {
public void doIt() {
給區域變數res
int result = res * 10;
….
}
}
}
表面上…你綁定了res
事實上...編譯器只是建立一個區域變數res
所以編譯器強迫你要加上 final
43. #int(int) doubler = #(int x)(2*x);
doubler.(3)
int doubler(int x) {
return 2 * x;
}
...
doubler(3);
#int(int, int) sum = #(int x, int y)(x+y);
int sum(int x, int y) {
return x + y;
}
44. Python
max = lambda m, n: m if m > n else n
max(10, 3)
Scala
val max: (Int, Int) => Int = (m, n) => if(m > n) m else n
max(10, 3);
Java
#int(int, int) max = #(int x, int y) {
if (x >= y) return x;
else return y;
};
max.(10, 3);
45. Scala
def selection(number: Array[Int], order: (Int, Int) => Boolean) {
...
val o = order(a, b)
…
}
…
selection(arr, (a, b) => a > b)
Java
void selection(int[] array, #boolean(int, int) order) {
...
boolean o = order.(a, b);
…
}
…
selection(arr, #(int a, int b)(a > b));
46. 目前 Java 沒有 lambda/closure
List<String> list = new ArrayList<String>();
list.add("Justin");
...
Collections.sort(list, new Comparator<String>() {
public int compare(String s1, String s2) {
return -s1.compareTo(s2);
}
…
});
假設 Java 有 lambda/closure
Collections.sort(list,
#(String s1, String s2)(-s1.compareTo(s2)));
49. void selection(int[] array, #boolean(int, int) order) {
...
boolean o = order.(a, b);
… Function type
}
…
selection(arr, #(int a, int b)(a > b));
public interface Order {
public boolean compare(int a, int b);
}
void selection(int[] array, Order order) {
...
boolean o = order.compare(a, b);
…
} SAM(Single Abstract Method) type
…
selection(arr, (a, b) -> {a > b}); More type inferencing
Scala-like syntax
50. val arr = Array(10, 20, 30)
var sum = 0
arr.foreach(i => sum += i)
println(sum)
int[] = {10, 20, 30};
int sum = 0;
arr.foreach(i -> { sum += i });
System.out.println(sum);
Much Better!!