Errata

Java in a Nutshell

Errata for Java in a Nutshell

Submit your own errata for this product.

The errata list is a list of errors and their corrections that were found after the product was released.

The following errata were submitted by our customers and have not yet been approved or disproved by the author or editor. They solely represent the opinion of the customer.

Color Key: Serious technical mistake Minor technical mistake Language or formatting error Typo Question Note Update

Version Location Description Submitted by Date submitted
Ex4-4
Example 4-4

In Example 4-4 there is code with a comment:

"int i = 0; / i not final; not usable by local class"

In the context of the example, the comment is incorrect. Since i is not modified after the local class is defined, it is effectively final, and therefore is accessible from within the local class. Tested and confirmed on jdk 11 by adding "System.out.println(i);" to the printVars() method.

Michael Koehmstedt  Oct 06, 2021 
Printed Page p. 41
3rd paragraph (or 5th depending on how you count paragraphs)

My printing (Second Release, 2019-06-21) shows this text, using the postfix *-crement operators:

"The expressions x++ and x-- are equivalent to x=x+1 and x=x-1, respectively [...]"

As expressions, these respective pairs don't evaluate to the same value. The text should probably use the prefix operators and read as this:

"The expressions ++x and --x are equivalent to x=x+1 and x=x-1, respectively [...]"

William Rummler  Nov 04, 2022 
Printed Page 92-93
Code blocks under "Comparing Objects"

The code block on page 92 has these lines:

if (s == t) System.out.println("equal"); // But they are not equal!

and

if (a == b) System.out.println("equal"); // But they are not equal!

It is unclear how these lines execute, without running the code. Much better would be something like:

System.out.println(s == t); // Prints "false".

because now the reader doesn't have to compile the code to see the result.

The same thing can be said for the code block on page 93 using s.equals(t) (just state in the comment what it evaluates to, instead of leaving up the guesswork to the reader).

Linus Arver  Jan 30, 2019 
Printed Page 123
1st paragraph under "Overriding Superclass Methods", 1st sentence

At least according to the example of the SportsCar class in the next page, it should mention that the "@Override" keyword is required to override the method of the superclass.

Linus Arver  Jan 30, 2019 
Printed Page 170
Halfway down the page

"Some developers also like to use the *single abstract method* (or SAM) type to refer to the interface type that the lambda is converted into."

This sentence is extremely confusing as written. I assume that the actual intended meaning is "Some developers also like to use the term *single abstract method* ...".

As currently phrased, it sounds as though this "single abstract method type" is a separate (though evidently related) concept which is name-dropped here but never defined.

Alexander Taylor  Aug 20, 2019 
Printed Page 204
After first sample code section

The description of the "decorator pattern" uses the example of an abstract class BurritoOptionalExtra which implements the Burrito interface.

The first sample code block defines the BurritoOptionalExtra class. This is followed by a note pointing out that it can only be used via subclasses. The second sample code block shows how said subclasses can be used. However, the subclasses themselves (Jalapeno and Guacamole) are never defined in the example.

While it is possible to extrapolate that Jalapeno and Guacamole are indeed subclasses of BurritoOptionalExtra, and the essentials of how they are implemented, it is unhelpful to leave this information out. Because the "decorator pattern" is actually being defined through the use of these examples (and not through descriptive text), the examples really need to be as complete as possible. Otherwise, the reader is forced to do double work: first to extrapolate the missing pieces of code, then to extrapolate the meaning of the "decorator pattern".

Providing this missing piece would improve the clarity of this section tremendously, at a cost of only a few extra lines.

Alexander Taylor  Sep 16, 2019 
Printed Page 285
5th para

'always', not 'never', "Java code, even when written in a functional style, will always be full of boilerplate,...

Dan Wilkin  Jan 27, 2023 
PDF Page 310
Code snippet inside the paragraph titled: The List Interface

I suppose that for java 11 at least, the line below must be corrected

String last = l.get(l.size - 1); // Last element of list

to the code below:

String last = l.get(l.size() - 1); // Last element of list

Thank you,
NA

Nick Aiva  Sep 08, 2019 
PDF Page 365
Code snippet inside the paragraph titled: Readers and Writers

Hi,
it seems that the given regex pattern "^#(\\w+)\\s*(\\w+)?" does not filter any symbols like !@#$%^&*()_+ as a meta.

Pattern SHELL_META_START = Pattern.compile("^#(\\w+)\\s*(\\w+)?");

I cannot get the Matcher to filter and separate characters from metas using the class below where the number 0 exits the programme.


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
*
* @author nicka
*/
public class ReadRegex {

public static void main(String[] args) {
Pattern SHELL_META_START = Pattern.compile("^#(\\w+)\\s*(\\w+)?");
try (BufferedReader console
= new BufferedReader(new InputStreamReader(System.in))) {
String line;
READ: while ((line = console.readLine()) != null && !"0".equals(line)) {
// Check for special commands ("metas")
Matcher m = SHELL_META_START.matcher(line);
if (m.find()) {
String metaName = m.group(1);
String arg = m.group(2);
//System.out.println("meta entered: " + metaName + " Text "+ arg);
doMeta(metaName, arg);
continue READ;
}
System.out.println("Text entered: "+line );
}
} catch (IOException e) {
// Handle FileNotFoundException, etc. here
e.printStackTrace();
}
}

public static void doMeta(String metaName, String arg) {
System.out.println("special commands: " + metaName);
System.out.println("Plain text: "+ arg);
}

}
Could you please offer a helping hand?
Thank you!
NA

Nick Aiva  Sep 13, 2019 
PDF Page 370
Code snippet inside the paragraph titled: Path

hi,
I suppose there is no p2 variable declared in the original code snippet:

Path p = Paths.get("/Users/ben/cluster.txt");
Path p = Paths.get(new URI("file:///Users/ben/cluster.txt"));
System.out.println(p2.equals(p));
File f = p.toFile();
System.out.println(f.isDirectory());
Path p3 = f.toPath();
System.out.println(p3.equals(p));

The correction follows:

Path p = Paths.get("/Users/ben/cluster.txt");
Path p2 = Paths.get(new URI("file:///Users/ben/cluster.txt"));
System.out.println(p2.equals(p));
File f = p.toFile();
System.out.println(f.isDirectory());
Path p3 = f.toPath();
System.out.println(p3.equals(p));

Thank you
NA

Nick Aiva  Sep 13, 2019 
Other Digital Version 417
Chapter 13, Sec. 'Command -line tools, Under the heading 'java' and subheading 'Basic usage'

In the example command:
"java some.package.MyClass java -jar my-packaged.jar"

What is the purpose of the word 'java' following '.MyClass' ?

(I am using the Google Books digital version.)
Thank You!

Joe Sock  Jun 14, 2019 
Other Digital Version 417
Chapter 13, Sec. 'Command -line tools, Under the heading 'java' and subheading 'Basic usage'

re: 14Jun2019 question about keyword, 'java' in java.exe command line exemplar: Sorry for the dumb question. Of coarse, 'java' is a stand-in for the name of the class holding the psvm() method to be called.

Joe Sock  Jun 20, 2019 
PDF Page 419
Second code snippet in paragraph Multi-Release JARs

According to the jdk 9 docs:

https://docs.oracle.com/javase/9/docs/api/java/lang/ProcessHandle.html

the correct method name is pid() not getPid(),
as wrongly mentioned in the book code given below:

public class GetPID {
public static long getPid() {
// Use new Java 9 Process API...
ProcessHandle processHandle = ProcessHandle.current();
return processHandle.getPid();
}
}

Thank you!
NA

Nick Aiva  Sep 22, 2019