forked from mvolkmann/mvolkmann.github.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJava5.html
More file actions
214 lines (204 loc) · 5.74 KB
/
Copy pathJava5.html
File metadata and controls
214 lines (204 loc) · 5.74 KB
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
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR.html1/DTD.html1-transitional.dtd">
<html xmlns="http://www.w3.org/1999.html">
<head>
<title>Java 5</title>
<link rel="stylesheet" type="text/css" href="../common.css"/>
</head>
<body>
<h2>Java 5</h2>
<p>
This page describe the major new features added in Java 5.
</p>
<h3>Annotations (Metadata)</h3>
<ul>
<li>
avoids need for external configuration files,
allowing configuration data to be embedded in source files
</li>
<li>
avoids duplicated boilerplate code by enabling tools to
generate it from annotations in source code
</li>
<li>
leads to a declarative programming style
</li>
<li>
an example of a specific annotation added in Java 5 is
<code>@Override</code> which marks a method that
overrides a superclass method
</li>
<li>
for more information, see
<a target="_blank" href="http://java.sun.com/j2se/1.5.0/docs/guide/language/annotations.html">here</a>
</li>
</ul>
<h3>Autoboxing/Unboxing</h3>
<ul>
<li>
avoids need to convert between primitives and
their corresponding type wrapper objects
</li>
<li>
basic example<br/>
<div class="code"><pre>
int i = 19;
Integer iObj = i;
int j = iObj;
// i and j are equal
</pre></div>
</li>
<li>
collections example<br/>
<div class="code"><pre>
List<Integer> list = new ArrayList<Integer>(); // see "Generics"
int i = 19;
list.add(i);
int j = list.get(0);
// i and j are equal
</pre></div>
</li>
<li>
overuse can result in performance issues
</li>
<li>
for more information, see
<a target="_blank" href="http://java.sun.com/j2se/1.5.0/docs/guide/language/autoboxing.html">here</a>
</li>
</ul>
<h3>Enhanced For Loop</h3>
<ul>
<li>
array example<br/>
<div class="code"><pre>
Car[] cars = new Car[](5);
// populate array of Car objects
for (Car car : cars) {
// Use the variable car inside the loop.
}
</pre></div>
</li>
<li>
list example<br/>
<div class="code"><pre>
List<Car> cars = new ArrayList<Car>() // see "Generics"
// populate List of Car objects
for (Car car : cars) {
// Use the variable car inside the loop.
}
</pre></div>
</li>
<li>
for more information, see
<a target="_blank" href="http://java.sun.com/j2se/1.5.0/docs/guide/language/foreach.html">here</a>
</li>
</ul>
<h3>Enums</h3>
<ul>
<li>
can create enumerated types with arbitrary fields and methods
</li>
<li>
example<br/>
<div class="code"><pre>
enum Season { WINTER, SPRING, SUMMER, FALL }
Season season = SPRING;
for (Season season : Season.values()) {
// use season variable
}
</pre></div>
</li>
<li>
to get the integer value of an enumerated value,
invoke the <code>ordinal</code> method on it
</li>
<li>
for more information, see
<a target="_blank" href="http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html">here</a>
(includes an example of adding fields and methods to enums)
</li>
</ul>
<h3>Generics</h3>
<ul>
<li>
most often used for type-safe collections
</li>
<li>
example<br/>
<div class="code"><pre>
List<String> myList = new ArrayList<String>();
// only String objects can be added to myList.
</pre></div>
</li>
<li>
collections of subclass objects can be passed to methods
that accept collections of superclass objects
(for example, an object of type
<code>List<Dog></code>
can be passed to a method that accepts
<code>List<? extends Animal></code>
or even
<code>List<? extends Object></code>)
</li>
<li>
for more information, see
<a target="_blank" href="http://java.sun.com/j2se/1.5.0/docs/guide/language/generics.html">here</a>
</li>
</ul>
<h3>Static Import</h3>
<ul>
<li>
avoids the need to qualify static members of
classes or interfaces with the class or interface name
</li>
<li>
example from JUnit 4<br/>
<div class="code"><pre>
import static org.junit.Assert.*;
// can now use static assert methods from the Assert class
// without prefixing them with "Assert."
</pre></div>
</li>
<li>
note that the ".*" part says to import all the static members
from the Assert class (can't omit that!)
</li>
<li>
for more information, see
<a target="_blank" href="http://java.sun.com/j2se/1.5.0/docs/guide/language/static-import.html">here</a>
</li>
</ul>
<h3>Varargs</h3>
<ul>
<li>
eliminates the need to put arguments into an array for the
purpose of passing a variable number of arguments to a method
</li>
<li>
example<br/>
<div class="code"><pre>
public void washCars(Car... cars) {
for (int i = 0; i < cars.length; i++) {
Car car = cars[i];
// wash the car
}
}
// call like this
washCars(car1, car2, car3);
</pre></div>
</li>
<li>
the type of cars is Car[]
</li>
<li>
for more information, see
<a target="_blank" href="http://java.sun.com/j2se/1.5.0/docs/guide/language/varargs.html">here</a>
</li>
</ul>
<hr/>
<p style="text-align:center">
Copyright © 2007 Object Computing, Inc. All rights reserved.
</p>
</body>
</html>