• 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

String Equality During Re-assignment  RSS feed

 
Greenhorn
Posts: 29
3
Linux
  • Mark post as helpful
  • send pies
  • 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!

 
Master Rancher
Posts: 3276
33
  • Mark post as helpful
  • send pies
  • 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
  • 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
Master Rancher
Posts: 3276
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the javap program in the JDK tools
 
Ranch Hand
Posts: 231
12
  • Likes 2
  • Mark post as helpful
  • send pies
  • 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.

 
Rancher
Posts: 2835
96
  • Mark post as helpful
  • send pies
  • 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
  • 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: 231
12
  • Mark post as helpful
  • send pies
  • 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
  • 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: 231
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome 
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!