• 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
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

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

 
Ranch Hand
Posts: 227
1
  • Likes 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: 71634
312
  • 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: 71634
312
  • 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: 227
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: 71634
312
  • 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: 40354
819
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.
 
Heroic work plunger man. Please allow me to introduce you to this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic