forked from hakimel/reveal.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjava.features.html
471 lines (434 loc) · 16.7 KB
/
java.features.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
<!doctype html>
<html lang="en" xmlns:util="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<title>Java 7,8 New Features</title>
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/theme/sky.css" id="theme">
<!-- Theme used for syntax highlighting of code -->
<link rel="stylesheet" href="lib/css/github.css">
<!-- Printing and PDF exports -->
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? 'css/print/pdf.css' : 'css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section>
<h2>Java 7,8 New Features</h2>
<p>
<small>Created by <i>Kevin Feng</i></small>
</p>
</section>
<section>
<h2>Agenda</h2>
<p><u>Language Specification</u></p>
<p>JVM Changes</p>
<p>Library & API</p>
<p>Misc</p>
</section>
<section>
<section>
<h2>Java 7 Language Specification</h2>
</section>
<section>
<p>Binary Literals</p>
<p>Underscores in Numeric Literals</p>
<pre><code class="hljs" data-trim contenteditable>
int binaryNumber = 0b011;
int underscoreNumber = 12_345_6789;
Assert.assertEquals(3, binaryNumber);
Assert.assertEquals(123456789, underscoreNumber);
</code></pre>
</section>
<section>
<p>Strings in switch Statements</p>
<pre><code class="hljs" data-trim contenteditable>
switch (reportDocument.getReportPeriod()){
case "day":
builder = CronScheduleBuilder.dailyAtHourAndMinute(hour, 0);
break;
case "week":
builder = CronScheduleBuilder.atHourAndMinuteOnGivenDaysOfWeek(hour, 0, day);
break;
case "month":
builder = CronScheduleBuilder.monthlyOnDayAndHourAndMinute(day, hour, 0);
break;
} </code></pre>
</section>
<section>
<p>Type Inference for Generic Instance Creation</p>
<pre><code class="hljs" data-trim contenteditable>
List<String> list = new ArrayList<>();
List<Map<String, String>> list2 = new ArrayList<>();
</code></pre>
</section>
<section>
<p>Non-Reifiable Formal Parameters with Varargs</p>
<pre><code class="hljs" data-trim contenteditable>
public static <T> void addToList (List<T> listArg, T... elements) {
for (T x : elements) {
listArg.add(x);
}
}
public static void faultyMethod(List<String>... l) {
Object[] objectArray = l; // Valid
objectArray[0] = Arrays.asList(new Integer(42));
String s = l[0].get(0); // ClassCastException thrown here
}
</code></pre>
<p>Heap pollution</p>
<p><small>@SafeVarargs or @SuppressWarnings({"unchecked", "varargs"})</small></p>
</section>
<section>
<p>The try-with-resources Statement</p>
<small>interface <code>java.lang.AutoCloseable</code></small>
<p><small>close immediately after try block</small></p>
<pre><code class="hljs" data-trim contenteditable>
try (Connection connection = getConnection();
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT sysdate FROM dual");) {
if (resultSet.next()) {
System.out.println(resultSet.getDate(1));
}
} catch (SQLException e) {
e.printStackTrace();
}
</code></pre>
<small>What if extra operation catch exception? -> <code>connection.rollback()</code></small>
</section>
<section>
<p>Try-with-resources - Solution 1</p>
<pre><code class="hljs" data-trim contenteditable>
try (Connection connection = getConnection()) {
connection.setAutoCommit(false);
try (Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT sysdate FROM dual")) {
if (resultSet.next()) {
System.out.println(resultSet.getDate(1));
}
} catch (SQLException e) {
connection.rollback();
e.printStackTrace();
}
} catch (SQLException e) {
e.printStackTrace();
}
</code></pre>
</section>
<section>
<p>Try-with-resources - Solution 2</p>
<pre><code class="hljs" data-trim contenteditable>
try (Connection connection = getConnection();
RollbackConnection rollbackConnection = new RollbackConnection(connection, false);
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT sysdate FROM dual");) {
if (resultSet.next()) {
System.out.println(resultSet.getDate(1));
}
rollbackConnection.commit();
} catch (SQLException e) {
e.printStackTrace();
}
public class RollbackConnection implements AutoCloseable {
private Connection connection;
private boolean autoCommit;
private boolean committed;
public RollbackConnection(Connection connection, boolean autoCommit) throws SQLException {
this.connection = connection;
this.autoCommit = autoCommit;
this.connection.setAutoCommit(autoCommit);
}
public void commit() throws SQLException {
if (!autoCommit) {
connection.commit();
}
committed = true;
}
@Override
public void close() throws SQLException {
if (!autoCommit && !committed) {
connection.rollback();
}
connection.close();
}
}
</code></pre>
</section>
<section>
<p>Catching Multiple Exception Types and Rethrowing Exceptions with Improved Type Checking</p>
<pre><code class="hljs" data-trim contenteditable>
private static void testMethod() throws ClassNotFoundException, FileNotFoundException {
try {
Class.forName("test");
new FileInputStream(new File("test"));
} catch (ClassNotFoundException | FileNotFoundException e) {
e.printStackTrace();
throw e;
}
}
</code></pre>
<small>Exception types must be disjoint</small>
</section>
</section>
<section>
<section>
<h2>Java 7 JVM Changes</h2>
</section>
<section>
<p>Java Virtual Machine Support for Non-Java Languages</p>
<p>Garbage-First Collector replaces the Concurrent Mark-Sweep Collector (CMS)</p>
<p>Java HotSpot Virtual Machine Performance Enhancements</p>
</section>
</section>
<section>
<section>
<h2>Java 7 Library & API</h2>
</section>
<section>
<h3>ForkJoinPool</h3>
<p>ForkJoinTask</p>
<p>fork()</p>
<p>join()</p>
<small>cannot cancel running task</small>
</section>
<section>
<h3>TransferQueue</h3>
<p>extends BlockingQueue</p>
<p>transfer()</p>
<p>producers may wait for consumers to receive elements</p>
</section>
<section>
<p>Mozilla Rhino JavaScript engine with modifications</p>
<p>Swing</p>
<p>Objects.*()</p>
</section>
</section>
<section>
<section>
<h2>Java 8 Language Specification</h2>
</section>
<section>
<h3>Lambda Expression</h3>
<h4>Functional Interface</h4>
<h4>Interface default/static method</h4>
<h4>Expression Syntax</h4>
<h4>Scope</h4>
</section>
<section>
<h3>Functional Interface</h3>
<p>only one abstract method</p>
<p>@FunctionalInterface contract</p>
</section>
<section>
<h3>Interface default/static method</h3>
<p>compatibility</p>
<p>funtional interface</p>
<p>extends from multiple interface?</p>
</section>
<section>
<h3>Lambda Expression</h3>
<pre><code class="hljs" data-trim contenteditable>
Collections.sort(stringList, new Comparator<String>() {
@Override
public int compare(String a, String b) {
return b.compareTo(a);
}
});
</code></pre>
<p>simplify anonymous inner class</p>
<p>designed to single responsibility</p>
</section>
<section>
<h3>Lambda Expression Syntax</h3>
<pre><code class="hljs" data-trim contenteditable>
Collections.sort(names, (String a, String b) -> {return b.compareTo(a);});
Collections.sort(names, (String a, String b) -> b.compareTo(a));
Collections.sort(names, (a, b) -> b.compareTo(a));
Collections.sort(names, (a, b) -> this::compareTo);
Collections.sort(names, (a, b) -> Comparator::compareTo);
int compareTo(String a, String b) {
return b.compareTo(a);
}
public class Comparator {
static int compareTo(String a, String b) {
return b.compareTo(a);
}
}
</code></pre>
</section>
<section>
<h3>Lambda expression scope</h3>
<p>local variable: effectively final</p>
<p>static variable</p>
<p>class variable</p>
<p>default method: no!</p>
</section>
<section>
<h3>Lambda expression advanced</h3>
<p>serialization</p>
<p>asm 5.x</p>
</section>
<section>
<h3>Stream - Creation</h3>
<p>java.util.stream.Stream</p>
<p>Stream.of()</p>
<p>Collection.stream() </p>
<p>Collection.parallelStream() </p>
<p>Arrays.stream() </p>
</section>
<section>
<h3>Stream API - intermediate operation</h3>
<ul>
<li> filter(Predicate predicate)</li>
<li> map(Function mapper)</li>
<li>flatMap(Function mapper)</li>
<li> distinct()</li>
<li> peek(Consumer action)</li>
<li> limit(long maxSize)</li>
<li> skip(long n)</li>
</ul>
</section>
<section>
<h3>Stream API - terminal operation</h3>
<ul>
<li> <u>forEach</u>(Consumer action)</li>
<li> <u>collect</u>(Collector collector)</li>
<li> toArray()</li>
<li> <u>reduce</u>(BinaryOperator accumulator)</li>
<li> min(Comparator comparator)</li>
<li> max(Comparator comparator)</li>
<li> count()</li>
</ul>
</section>
<section>
<h3>Stream API - lazy computation</h3>
<p>efficiency</p>
<pre><code class="hljs" data-trim contenteditable>
Stream.of("one", "two", "three", "four")
.filter(e -> e.length() > 3)
.peek(e -> System.out.println("Filtered value: " + e))
.map(String::toUpperCase)
.peek(e -> System.out.println("Mapped value: " + e))
.collect(Collectors.toList());
</code></pre>
<p>result?</p>
</section>
<section>
<h3>Stream API - useful API</h3>
<p>Optional</p>
<p>Spliterator</p>
<p>primitive type new APIs</p>
<p>BufferedReader.lines()</p>
<p>ThreadLocal.withInitial()</p>
<p>Iterable.forEach()</p>
<p>List.sort()</p>
<p>Map.computeIfAbsent()...</p>
</section>
<section>
<h3>Repeating Annotations</h3>
<pre><code class="hljs" data-trim contenteditable>
@Schedule(dayOfMonth="last")
@Schedule(dayOfWeek="Fri", hour="23")
public void doPeriodicCleanup() { ... }
</code></pre>
</section>
<section>
<h3>Method Parameter Reflection</h3>
<p>Executable.getParameters()</p>
<p>get parameter name</p>
<p>compile with -parameters option</p>
</section>
<section>
<h3>Date-Time Package</h3>
<p>similar to joda-time</p>
<p>immutable instance</p>
</section>
</section>
<section>
<section>
<h2>Java 8 JVM Changes</h2>
</section>
<section>
<h3>Removal of PermGen</h3>
<p>Meta Space instead</p>
</section>
</section>
<section>
<section>
<h2>Java 8 Library & API</h2>
</section>
<section>
<h3>Collections</h3>
<p>Aggregate operations</p>
<p>HashMap key collision improvement</p>
</section>
<section>
<h3>Compact Profiles</h3>
<p>a subset of the full Java SE Platform API</p>
<p>compact1, compact2, and compact3</p>
<p>compile with -profile option</p>
</section>
<section>
<p>Concurrency Utilities Enhancement</p>
<p>The JDBC-ODBC Bridge has been removed</p>
<p>Nashorn ECMAScript engine</p>
<p>JavaFX</p>
</section>
</section>
<section>
<section>
<h2>Java 8 Misc</h2>
</section>
<section>
<p>Java Deployment</p>
</section>
</section>
<section>
<h2>Reference</h2>
<a>http://www.oracle.com/technetwork/java/javase/8-whats-new-2157071.html</a>
<a>http://docs.oracle.com/javase/8/docs/technotes/guides/index.html#javalanguage</a>
<a>http://docs.oracle.com/javase/8/docs/technotes/guides/language/enhancements.html</a>
</section>
<section>
<h1>THE END</h1>
</section>
</div>
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.js"></script>
<script>
// More info https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
width: 1024,
height: 768,
controls: true,
progress: true,
history: true,
center: true,
transition: 'slide', // none/fade/slide/convex/concave/zoom
// More info https://github.com/hakimel/reveal.js#dependencies
dependencies: [
{ src: 'lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'plugin/markdown/marked.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: 'plugin/zoom-js/zoom.js', async: true },
{ src: 'plugin/notes/notes.js', async: true }
]
});
</script>
</body>
</html>