This week's book giveaways are in the Angular and TypeScript and Web Services forums.
We're giving away four copies each of Programming with Types and The Design of Web APIs and have the authors on-line!
See this thread and this one for details.
Win a copy of Programming with Types this week in the Angular and TypeScript forum
or The Design of Web APIs in the Web Services forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Joe Ess
  • salvin francis

Possible Errata for OCP Java SE 11 Programmer I Study Guide Page 179

 
Ranch Hand
Posts: 179
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I refer to the following codes on page 179 of the OCP Java SE11 Programmer I Study Guide by Jeanne Boyarsky and Scott Selikoff.

String singleString = "hello world";
String oneLine = "hello" + "world";
String concat = " hello";
concat += "world";
System.out.println(singleString == oneLine);
System.out.println(singleString == concat);

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.
 
Marshal
Posts: 66980
255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 66980
255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With a subtle change to line 2, that should now print true.
 
Edmund Yong
Ranch Hand
Posts: 179
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:With a subtle change to line 2, that should now print true.



There is indeed a space after "hello" for oneLine in the book. So the first System.out.println should print true, not false as stated in the book.
 
Campbell Ritchie
Marshal
Posts: 66980
255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
author & internet detective
Posts: 39760
797
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
This is my favorite tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!