• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String Equality During Re-assignment

 
Greenhorn
Posts: 29
3
Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I came across this and it is throwing me off:
Prints false.  But I dont understand why.  By the time str1.equals( ... ) is called, str1 is pointing to the equivalent string in the pool.

Thank you Enthuware for this puzzler!

 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It must be that The assignment takes place before the equals method evaluates using the value of str1 before the equals is called.

Have you looked at the generated code to see what order the byte-code statements are executed?
 
John Carlos
Greenhorn
Posts: 29
3
Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:
Have you looked at the generated code to see what order the byte-code statements are executed?


No.  What is this dark magic you speak of?  (Translation: how do I do that?)
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at the javap program in the JDK tools
 
Ranch Hand
Posts: 234
12
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It makes sense that a method's argument is fully evaluated before the method executes.

According to the JLS

At run time, method invocation requires five steps. First, a target reference may be computed. Second, the argument expressions are evaluated. Third, the accessibility of the method to be invoked is checked. Fourth, the actual code for the method to be executed is located. Fifth, a new activation frame is created, synchronization is performed if necessary, and control is transferred to the method code.

 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This problem is similar to the many times asked question about the famous 'i = i++'. Search the OCAJP forum for many explanations.
A similar question is:
 
John Carlos
Greenhorn
Posts: 29
3
Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daniel Cox wrote: According to the JLS


Thanks Daniel for linking this document.  I didn't really understand the run time blurb when I read it(and franky, I'm still not sure I do).  But there's an example on that page and I'll paste in the explanation in case anyone else comes across this thread looking for the answer...

I've edited the method and variable names to match my question:

The occurrence of str1 before ".equals" is evaluated first, before the argument expression str1 = str2. Therefore, a reference to the string "one" is remembered as the target reference before the local variable str1 is changed to refer to the string "two". As a result, the equals method is invoked for target object "one" with argument "two", so the result of the invocation is false.

 
Daniel Cox
Ranch Hand
Posts: 234
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For reference purposes, the example John is referring to is Example 15.12.4.1-2. Evaluation Order During Method Invocation


 
John Carlos
Greenhorn
Posts: 29
3
Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daniel Cox wrote:For reference purposes, the example John is referring to is Example 15.12.4.1-2. Evaluation Order During Method Invocation



Thanks again, Daniel.  I looked for an anchor tag but didn't find one.
 
Daniel Cox
Ranch Hand
Posts: 234
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome  
 
The knights of nee want a shrubbery. And a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic