Help coderanch get a
new server
by contributing to the fundraiser
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

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

 
Ranch Hand
Posts: 229
1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79630
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79630
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With a subtle change to line 2, that should now print true.
 
Edmund Yong
Ranch Hand
Posts: 229
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79630
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 41964
911
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic