The book states that "Both print false. Concatenation is just like calling a method and results in a new String."
This is not correct, right? I thought the first System.out.println should print true. This is quite puzzling because on the next page, there is a similar example and an explanation that a concatenated string literal will be in the string pool.
Please verify that you have copied the code exactly from the book. You have added and removed spaces so all your concatenated Strings will be different from one another. Even if you make yoiur spaces consistent, please remember that a String is only interned if you call the intern() method, or if it is a compile‑time constant. Remind yourself of the rules about compile‑time constants.
Edmund Yong wrote:. . . the first System.out.println should print true, not false as stated in the book.
Please verify that there are no other spelling differences from what you posted; maybe you have indeed found a “new” erratum. Please check here that I am thinking of the same book. One of the authors (JB) checks these fora regularly for errata.
You did in fact find an erratum. It's an interesting one.
The exact code in the book is:
I traced it through our drafts to see where it originated. We originally had two spaces instead of one. So when we tested it, it printed false for both. In PDF review, we caught the extra space. But we didn't re-run so didn't realize that made the first println true. (The second println is definitely true). Adding to errata now.