Further Updates prior to releasing Version 2.0 of book

This commit is contained in:
Bruce Eckel 2021-12-14 12:38:46 -07:00
parent 7bf3a4c7b7
commit ade1d5ef12
15 changed files with 46 additions and 100097 deletions

View File

@ -24,7 +24,7 @@ public class ApplesAndOrangesWithoutGenerics {
apples.add(new Orange()); apples.add(new Orange());
for(Object apple : apples) { for(Object apple : apples) {
((Apple) apple).id(); ((Apple) apple).id();
// Orange is detected only at run time // Orange is detected only at runtime
} }
} }
} }

View File

@ -16,19 +16,23 @@ public class AsListInference {
List<Snow> snow1 = Arrays.asList( List<Snow> snow1 = Arrays.asList(
new Crusty(), new Slush(), new Powder()); new Crusty(), new Slush(), new Powder());
//- snow1.add(new Heavy()); // Exception //- snow1.add(new Heavy()); // Exception
//- snow1.remove(0); // Exception
List<Snow> snow2 = Arrays.asList( List<Snow> snow2 = Arrays.asList(
new Light(), new Heavy()); new Light(), new Heavy());
//- snow2.add(new Slush()); // Exception //- snow2.add(new Slush()); // Exception
//- snow2.remove(0); // Exception
List<Snow> snow3 = new ArrayList<>(); List<Snow> snow3 = new ArrayList<>();
Collections.addAll(snow3, Collections.addAll(snow3,
new Light(), new Heavy(), new Powder()); new Light(), new Heavy(), new Powder());
snow3.add(new Crusty()); snow3.add(new Crusty());
snow3.remove(0);
// Hint with explicit type argument specification: // Hint with explicit type argument specification:
List<Snow> snow4 = Arrays.<Snow>asList( List<Snow> snow4 = Arrays.<Snow>asList(
new Light(), new Heavy(), new Slush()); new Light(), new Heavy(), new Slush());
//- snow4.add(new Powder()); // Exception //- snow4.add(new Powder()); // Exception
//- snow4.remove(0); // Exception
} }
} }

View File

@ -1,10 +0,0 @@
0: main
1: main
2: main
3: main
4: main
5: main
6: main
7: main
8: main
9: main

File diff suppressed because it is too large Load Diff

View File

@ -52,13 +52,12 @@ public class AddAndSubtractPaths {
} }
} }
/* Output: /* Output:
Windows 8.1 Windows 10
C:\Git C:\Git
(1)r OnJava8\ExtractedExamples\files\AddAndSubtractPath (1)r OnJava8\ExtractedExamples\files\AddAndSubtractPaths.java
s.java RealPath:
RealPath: C:\Git\OnJava8\ExtractedExamples\files\AddAnd C:\Git\OnJava8\ExtractedExamples\files\AddAndSubtractPaths.java
SubtractPaths.java (2)r OnJava8\ExtractedExamples\files
(2)r OnJava8\ExtractedExamples\strings\..\files
RealPath: C:\Git\OnJava8\ExtractedExamples\files RealPath: C:\Git\OnJava8\ExtractedExamples\files
(3)r OnJava8\ExtractedExamples\files (3)r OnJava8\ExtractedExamples\files
RealPath: C:\Git\OnJava8\ExtractedExamples\files RealPath: C:\Git\OnJava8\ExtractedExamples\files
@ -68,7 +67,7 @@ RealPath: C:\Git\OnJava8
RealPath: C:\Git\OnJava8 RealPath: C:\Git\OnJava8
(6)r OnJava8 (6)r OnJava8
RealPath: C:\Git\OnJava8 RealPath: C:\Git\OnJava8
(7)r OnJava8\ExtractedExamples\files\.\..\.. (7)r OnJava8
RealPath: C:\Git\OnJava8 RealPath: C:\Git\OnJava8
(8)r OnJava8 (8)r OnJava8
RealPath: C:\Git\OnJava8 RealPath: C:\Git\OnJava8

View File

@ -26,20 +26,22 @@ public class FileSystemDemo {
} }
} }
/* Output: /* Output:
Windows 8.1 Windows 10
File Store: (C:) File Store: (C:)
File Store: System Reserved (E:) File Store: System Reserved (E:)
File Store: (F:) File Store: (F:)
File Store: Google Drive (G:)
Root Directory: C:\ Root Directory: C:\
Root Directory: D:\ Root Directory: D:\
Root Directory: E:\ Root Directory: E:\
Root Directory: F:\ Root Directory: F:\
Root Directory: G:\
Separator: \ Separator: \
UserPrincipalLookupService: UserPrincipalLookupService:
sun.nio.fs.WindowsFileSystem$LookupService$1@19e0bfd sun.nio.fs.WindowsFileSystem$LookupService$1@1bd4fdd
isOpen: true isOpen: true
isReadOnly: false isReadOnly: false
FileSystemProvider: FileSystemProvider:
sun.nio.fs.WindowsFileSystemProvider@139a55 sun.nio.fs.WindowsFileSystemProvider@55183b20
File Attribute Views: [owner, dos, acl, basic, user] File Attribute Views: [owner, dos, acl, basic, user]
*/ */

View File

@ -23,7 +23,7 @@ public class PartsOfPaths {
} }
} }
/* Output: /* Output:
Windows 8.1 Windows 10
Git Git
OnJava8 OnJava8
ExtractedExamples ExtractedExamples

View File

@ -38,7 +38,7 @@ public class PathAnalysis {
} }
} }
/* Output: /* Output:
Windows 8.1 Windows 10
Exists: true Exists: true
Directory: false Directory: false
Executable: true Executable: true
@ -49,8 +49,8 @@ notExists: false
Hidden: false Hidden: false
size: 1617 size: 1617
FileStore: (C:) FileStore: (C:)
LastModified: : 2021-01-24T15:48:37.461504Z LastModified: : 2021-11-08T00:34:52.693768Z
Owner: GROOT\Bruce (User) Owner: GROOT\Bruce (User)
ContentType: null ContentType: text/plain
SymbolicLink: false SymbolicLink: false
*/ */

View File

@ -9,17 +9,17 @@ import java.io.IOException;
public class PathInfo { public class PathInfo {
static void show(String id, Object p) { static void show(String id, Object p) {
System.out.println(id + ": " + p); System.out.println(id + p);
} }
static void info(Path p) { static void info(Path p) {
show("toString", p); show("toString:\n ", p);
show("Exists", Files.exists(p)); show("Exists: ", Files.exists(p));
show("RegularFile", Files.isRegularFile(p)); show("RegularFile: ", Files.isRegularFile(p));
show("Directory", Files.isDirectory(p)); show("Directory: ", Files.isDirectory(p));
show("Absolute", p.isAbsolute()); show("Absolute: ", p.isAbsolute());
show("FileName", p.getFileName()); show("FileName: ", p.getFileName());
show("Parent", p.getParent()); show("Parent: ", p.getParent());
show("Root", p.getRoot()); show("Root: ", p.getRoot());
System.out.println("******************"); System.out.println("******************");
} }
public static void main(String[] args) { public static void main(String[] args) {
@ -37,15 +37,16 @@ public class PathInfo {
System.out.println(e); System.out.println(e);
} }
URI u = p.toUri(); URI u = p.toUri();
System.out.println("URI: " + u); System.out.println("URI:\n" + u);
Path puri = Paths.get(u); Path puri = Paths.get(u);
System.out.println(Files.exists(puri)); System.out.println(Files.exists(puri));
File f = ap.toFile(); // Don't be fooled File f = ap.toFile(); // Don't be fooled
} }
} }
/* Output: /* Output:
Windows 8.1 Windows 10
toString: C:\path\to\nowhere\NoFile.txt toString:
C:\path\to\nowhere\NoFile.txt
Exists: false Exists: false
RegularFile: false RegularFile: false
Directory: false Directory: false
@ -54,7 +55,8 @@ FileName: NoFile.txt
Parent: C:\path\to\nowhere Parent: C:\path\to\nowhere
Root: C:\ Root: C:\
****************** ******************
toString: PathInfo.java toString:
PathInfo.java
Exists: true Exists: true
RegularFile: true RegularFile: true
Directory: false Directory: false
@ -64,7 +66,7 @@ Parent: null
Root: null Root: null
****************** ******************
toString: toString:
C:\Git\OnJava8\ExtractedExamples\files\PathInfo.java C:\Git\OnJava8\ExtractedExamples\files\PathInfo.java
Exists: true Exists: true
RegularFile: true RegularFile: true
Directory: false Directory: false
@ -73,7 +75,8 @@ FileName: PathInfo.java
Parent: C:\Git\OnJava8\ExtractedExamples\files Parent: C:\Git\OnJava8\ExtractedExamples\files
Root: C:\ Root: C:\
****************** ******************
toString: C:\Git\OnJava8\ExtractedExamples\files toString:
C:\Git\OnJava8\ExtractedExamples\files
Exists: true Exists: true
RegularFile: false RegularFile: false
Directory: true Directory: true
@ -83,7 +86,7 @@ Parent: C:\Git\OnJava8\ExtractedExamples
Root: C:\ Root: C:\
****************** ******************
toString: toString:
C:\Git\OnJava8\ExtractedExamples\files\PathInfo.java C:\Git\OnJava8\ExtractedExamples\files\PathInfo.java
Exists: true Exists: true
RegularFile: true RegularFile: true
Directory: false Directory: false
@ -92,7 +95,7 @@ FileName: PathInfo.java
Parent: C:\Git\OnJava8\ExtractedExamples\files Parent: C:\Git\OnJava8\ExtractedExamples\files
Root: C:\ Root: C:\
****************** ******************
URI: file:///C:/Git/OnJava8/ExtractedExamples/files/Pat URI:
hInfo.java file:///C:/Git/OnJava8/ExtractedExamples/files/PathInfo.java
true true
*/ */

View File

@ -2,7 +2,6 @@
// (c)2021 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
import java.util.*;
interface Callable { // [1] interface Callable { // [1]
void call(String s); void call(String s);

View File

@ -23,7 +23,7 @@ implements Supplier<Coffee>, Iterable<Coffee> {
return (Coffee) return (Coffee)
types[rand.nextInt(types.length)] types[rand.nextInt(types.length)]
.getConstructor().newInstance(); .getConstructor().newInstance();
// Report programmer errors at run time: // Report programmer errors at runtime:
} catch(InstantiationException | } catch(InstantiationException |
NoSuchMethodException | NoSuchMethodException |
InvocationTargetException | InvocationTargetException |

View File

@ -1 +0,0 @@
Some text Some more

View File

@ -1,25 +0,0 @@
// references/ImmutableStrings.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// Demonstrating StringBuilder
public class ImmutableStrings {
public static void main(String[] args) {
String foo = "foo";
String s = "abc" + foo + "def"
+ Integer.toString(47);
System.out.println(s);
// The "equivalent" using StringBuilder:
StringBuilder sb =
new StringBuilder("abc"); // Creates String
sb.append(foo);
sb.append("def"); // Creates String
sb.append(Integer.toString(47));
System.out.println(sb);
}
}
/* Output:
abcfoodef47
abcfoodef47
*/

View File

@ -1,22 +0,0 @@
// references/Stringer.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
public class Stringer {
public static String upcase(String s) {
return s.toUpperCase();
}
public static void main(String[] args) {
String q = new String("howdy");
System.out.println(q); // howdy
String qq = upcase(q);
System.out.println(qq); // HOWDY
System.out.println(q); // howdy
}
}
/* Output:
howdy
HOWDY
howdy
*/

View File

@ -7,10 +7,10 @@ import java.util.stream.*;
public class RandomGenerators { public class RandomGenerators {
public static <T> void show(Stream<T> stream) { public static <T> void show(Stream<T> stream) {
stream stream
.limit(4) .limit(4)
.forEach(System.out::println); .forEach(System.out::println);
System.out.println("++++++++"); System.out.println("++++++++");
} }
public static void main(String[] args) { public static void main(String[] args) {
Random rand = new Random(47); Random rand = new Random(47);