This week's book giveaway is in the Java 9 forum.
We're giving away four copies of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications and have Sander Mak & Paul Bakker on-line!
See this thread for details.
Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

String Equality During Re-assignment  RSS feed

 
John Carlos
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!

 
Norm Radder
Rancher
Posts: 2240
28
  • 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
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the javap program in the JDK tools
 
Daniel Cox
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.
 
Piet Souris
Rancher
Posts: 2014
71
  • 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 
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!