• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Java 11 (String compile time constant vs concatenation '+')

 
Greenhorn
Posts: 7
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am reading OCP Oracle Certified Professional Java SE 11 programmer study guide IZO-815. Can anyone please help me with the following question -

Reference 1 - Page 179 - Last Paragraph - "Concatenation is like calling a method and results in a new String".

Reference 2 - Page 180 - 3rd Paragraph -  "We have a compile time constant that automatically gets placed in the string pool as "rat1"".


In reference 1 - is false. However, in reference 2, is true.

Can anyone explain ? What am I missing ?

I wrote a sample code by adding str in switch case statement and also checking == on two strings created using "+". It does seem that "+" is compile time. If so, what does Reference 1 mean ?
78A904CA-134E-4D82-B38E-CD3ADE7FE5ED.jpeg
[Thumbnail for 78A904CA-134E-4D82-B38E-CD3ADE7FE5ED.jpeg]
1B3E7B95-2255-44AA-9261-1BA3F94E8659.jpeg
[Thumbnail for 1B3E7B95-2255-44AA-9261-1BA3F94E8659.jpeg]
 
Ranch Hand
Posts: 477
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Radhika Ganesan wrote:Reference 1 - Page 179 - Last Paragraph - "Concatenation is like calling a method and results in a new String".


string1 + string2 is equivalent to string1.concat(string2)

Radhika Ganesan wrote:I wrote a sample code by adding str in switch case statement and also checking == on two strings created using "+". It does seem that "+" is compile time. If so, what does Reference 1 mean ?



Secondly, operators, like "+" are operators.  They cannot be classified as runtime or compile time.  The operands, however, maybe classified as runtime or compile time based upon the compiler's ability or not to determine value(s).

I hope that helps.
 
Radhika Ganesan
Greenhorn
Posts: 7
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Charles,

Your response does help a little but I would like to add more details to my initial question. Please find attached screen shots of the content I am reading-

Why is the code printing true at one instance and other false in the other ?

Thank you !
F835007A-51E2-44EA-8A42-5205D34C7CE8.jpeg
[Thumbnail for F835007A-51E2-44EA-8A42-5205D34C7CE8.jpeg]
218AD221-4F17-4590-B4A6-9805CC7C0F75.jpeg
[Thumbnail for 218AD221-4F17-4590-B4A6-9805CC7C0F75.jpeg]
 
Charles O'Leary
Ranch Hand
Posts: 477
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Radhika Ganesan wrote:Why is the code printing true at one instance


able to match "rat1" with "rat1" from the string pool

and other false in the other ?


not able to match "Hello world" with "Hello " and "world" from the string pool. You did notice interning can help with this, depending upon the requirement.
 
Marshal
Posts: 74376
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It has to do with whether the + operator is or isn't executed at compile time; it is iff the whole expression is a constant expression (=compile‑time constant). That link tells you what is and isn't a compile‑time constant, and compile‑time constant Strings are interned automatically. That is one of the things determining whether the == same object operator returns true or false.
 
Charles O'Leary
Ranch Hand
Posts: 477
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Radhika,
Put simply, the most important point here is timing.  At the point of evaluating "==",  can both operands on the left and right be found in the string pool?
 
Master Rancher
Posts: 4062
56
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Charles O'Leary wrote:

Radhika Ganesan wrote:Reference 1 - Page 179 - Last Paragraph - "Concatenation is like calling a method and results in a new String".


string1 + string2 is equivalent to string1.concat(string2)


Unless you are talking about whether they result in a new String, or a reference to a preexisting String in the pool. Then, they may be different.  A + operator can be part of a compile-time constant expression, if the operands are also constants or constant expressions.  But once you include a method call like concat(), it cannot be part of a compile-time expression.  See Campbell's link for details.

I would also note that the book quote has a mistake.  Given this code:

It says "Both print false."  Well, no - the first one prints true.  This has been corrected in the errata, which you (Radhika) should definitely make use of.  The authors have put a lot of effort into fixing any mistakes that have been discovered.
 
Saloon Keeper
Posts: 1646
61
Eclipse IDE Postgres Database C++ Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did y'all miss the errata here?

https://www.selikoff.net/ocp11-1/

People said several true things, but apply the errata given on that official errata page and see if you have any remaining confusion.
 
Charles O'Leary
Ranch Hand
Posts: 477
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:

Charles O'Leary wrote:

Radhika Ganesan wrote:Reference 1 - Page 179 - Last Paragraph - "Concatenation is like calling a method and results in a new String".


string1 + string2 is equivalent to string1.concat(string2)


Unless you are talking about whether they result in a new String, or a reference to a preexisting String in the pool. Then, they may be different.  A + operator can be part of a compile-time constant expression, ir the operands are also constants or constant expressions.  But once you include a method call like concat(), it cannot be part of a compile-time expression.  See Campbell's link for details.

I would also note that the book quote has a mistake.


My focus was on the word "like"... so I'll alter that to "string1 + string2 is like  string1.concat(string2)"  And also thanks for pointing out the Errata Mike and Jesse!
 
Radhika Ganesan
Greenhorn
Posts: 7
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for your responses and letting me know about errata !
reply
    Bookmark Topic Watch Topic
  • New Topic