Skip to content

Commit 8219ea0

Browse files
author
Bruce Eckel
committed
Book edits
1 parent 634d5df commit 8219ea0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+202
-212
lines changed

arrays/ArrayOfGenericType.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ public class ArrayOfGenericType<T> {
88
T[] array; // OK
99
@SuppressWarnings("unchecked")
1010
public ArrayOfGenericType(int size) {
11-
//! array = new T[size]; // Illegal
11+
//- array = new T[size]; // Illegal
1212
array = (T[])new Object[size]; // "unchecked" Warning
1313
}
1414
// Illegal:
15-
//! public <U> U[] makeArray() { return new U[10]; }
15+
//- public <U> U[] makeArray() { return new U[10]; }
1616
}

arrays/ArrayOfGenerics.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public static void main(String[] args) {
1313
ls = (List<String>[])la; // "Unchecked" warning
1414
ls[0] = new ArrayList<>();
1515
// Compile-time checking produces an error:
16-
//! ls[1] = new ArrayList<Integer>();
16+
//- ls[1] = new ArrayList<Integer>();
1717

1818
// The problem: List<String> is a subtype of Object
1919
Object[] objects = ls; // So assignment is OK

arrays/ArrayOptions.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ public static void main(String[] args) {
4949
g[i] = i*i;
5050
int[] h = { 11, 47, 93 };
5151

52-
// Compile error: variable e not initialized:
53-
//!print("e.length = " + e.length);
52+
// Compile error: variable e not initialized:
53+
//- print("e.length = " + e.length);
5454
System.out.println("f.length = " + f.length);
5555
System.out.println("g.length = " + g.length);
5656
System.out.println("h.length = " + h.length);

collections/ArrayIsNotIterable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public static void main(String[] args) {
1313
test(Arrays.asList(1, 2, 3));
1414
String[] strings = { "A", "B", "C" };
1515
// An array works in for-in, but it's not Iterable:
16-
//! test(strings);
16+
//- test(strings);
1717
// You must explicitly convert it to an Iterable:
1818
test(Arrays.asList(strings));
1919
}

collectionsindepth/ReadOnly.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@ public static void main(String[] args) {
1414
Collections.unmodifiableCollection(
1515
new ArrayList<>(data));
1616
System.out.println(c); // Reading is OK
17-
//! c.add("one"); // Can't change it
17+
//- c.add("one"); // Can't change it
1818

1919
List<String> a = Collections.unmodifiableList(
2020
new ArrayList<>(data));
2121
ListIterator<String> lit = a.listIterator();
2222
System.out.println(lit.next()); // Reading is OK
23-
//! lit.add("one"); // Can't change it
23+
//- lit.add("one"); // Can't change it
2424

2525
Set<String> s = Collections.unmodifiableSet(
2626
new HashSet<>(data));
2727
System.out.println(s); // Reading is OK
28-
//! s.add("one"); // Can't change it
28+
//- s.add("one"); // Can't change it
2929

3030
// For a SortedSet:
3131
Set<String> ss = Collections.unmodifiableSortedSet(
@@ -34,7 +34,7 @@ public static void main(String[] args) {
3434
Map<String,String> m = Collections.unmodifiableMap(
3535
new HashMap<>(Countries.capitals(6)));
3636
System.out.println(m); // Reading is OK
37-
//! m.put("Ralph", "Howdy!");
37+
//- m.put("Ralph", "Howdy!");
3838

3939
// For a SortedMap:
4040
Map<String,String> sm =

concurrency/ResponsiveUI.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public void run() {
2727
}
2828
}
2929
public static void main(String[] args) throws Exception {
30-
//! new UnresponsiveUI(); // Must kill this process
30+
//- new UnresponsiveUI(); // Must kill this process
3131
new ResponsiveUI();
3232
System.in.read();
3333
System.out.println(d); // Shows progress

control/BreakAndContinue.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,22 @@
77

88
public class BreakAndContinue {
99
public static void main(String[] args) {
10-
for(int i = 0; i < 100; i++) {
10+
for(int i = 0; i < 100; i++) { // (1)
1111
if(i == 74) break; // Out of for loop
1212
if(i % 9 != 0) continue; // Next iteration
1313
System.out.print(i + " ");
1414
}
1515
System.out.println();
1616
// Using for-in:
17-
for(int i : range(100)) {
17+
for(int i : range(100)) { // (2)
1818
if(i == 74) break; // Out of for loop
1919
if(i % 9 != 0) continue; // Next iteration
2020
System.out.print(i + " ");
2121
}
2222
System.out.println();
2323
int i = 0;
2424
// An "infinite loop":
25-
while(true) {
25+
while(true) { // (3)
2626
i++;
2727
int j = i * 27;
2828
if(j == 1269) break; // Out of loop

control/IfElse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class IfElse {
88
static void test(int testval, int target) {
99
if(testval > target)
1010
result = +1;
11-
else if(testval < target)
11+
else if(testval < target) // (1)
1212
result = -1;
1313
else
1414
result = 0; // Match
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
// control/IfElse2.java
1+
// control/TestWithReturn.java
22
// (c)2016 MindView LLC: see Copyright.txt
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
55

6-
public class IfElse2 {
6+
public class TestWithReturn {
77
static int test(int testval, int target) {
88
if(testval > target)
99
return +1;
10-
else if(testval < target)
10+
if(testval < target)
1111
return -1;
12-
else
13-
return 0; // Match
12+
return 0; // Match
1413
}
1514
public static void main(String[] args) {
1615
System.out.println(test(10, 5));

control/TrueFalse.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// control/TrueFalse.java
2+
// (c)2016 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
5+
6+
public class TrueFalse {
7+
public static void main(String[] args) {
8+
System.out.println(1 == 1);
9+
System.out.println(1 == 2);
10+
}
11+
}
12+
/* Output:
13+
true
14+
false
15+
*/

0 commit comments

Comments
 (0)