• 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/Thread question

 
Ranch Hand
Posts: 49
Eclipse IDE Spring Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greetings people

I've hard time understanding a probably quite simple question. Yet I tried to discuss it with a friend but he was not of help , making him have doubts about ( bad luck ! )

This is the question :



At somepoint shouldn't JVM simply start thread and execute it with run procedure (when it wants since we have no clue of whet it will be) , and change the reference of sName variable? I do understand String are inmutable , no changes. But the loop inside run() procedure do have to start at some point so there , sName's reference changes, from it's first value to a new String. Even more, I believe sName loses its first reference due concatenation at first call of the setter method (nameTest(String sName) ) (besides the coincidence of naming parameter as static class variable ) .
Am I wrong? or it doesn't apply?. Perhaps I am not approaching the question in the right way.

Thank you for your time.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please QuoteYourSources
 
Ranch Hand
Posts: 47
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Like you say, the reason is that String object is an immutable Object so if a method modify an immutable Object, this is a new reference and it doesn't modify your initial Object in main method.

It's like :

 
Guillaume Jourdan
Ranch Hand
Posts: 47
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, if you use StringBuffer Object with your first code, you obtain a different result.
 
David Samer
Ranch Hand
Posts: 49
Eclipse IDE Spring Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That I know but doesn't it lose the reference inside run() method ? . String sName do get reference to "good", but if I do use "+" operator , as for example, sName + = "thanks" , wouldn't sName variable have new reference being "good thanks" , instead of "good" that has been lost?
.

Also run() method is never called? or it really doesn't matter?


Thank you in advance.

PS: I'll use code instead next time, sorry. (edited due PS and grammar)
 
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is the sname in nameTest, the same as the static variable sname? This should answer your question.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Samer wrote:That I know but doesn't it lose the reference inside run() method ? . String sName do get reference to "good", but if I do use "+" operator , as for example, sName + = "thanks" , wouldn't sName variable have new reference being "good thanks" , instead of "good" that has been lost?
.



sName in nameTest method is refering to (String sName) method local variable ( basically you are declaring the method local variable and shadowing the static variable sName) .. so when you do + on sName you are actually referring to sName declared in nameTest not the static sName.

David Samer wrote:
Also run() method is never called? or it really doesn't matter?



The thread starts but even before the sName static variable is updated the output is printed "good". if you put debugger on code at start() method or if you add t.join() after line 6 .. you will see that it will print "good 0 1 2 3" . If you change the line 11 to Test.sName then the out put will be "good idea" : refering the static sName.

 
David Samer
Ranch Hand
Posts: 49
Eclipse IDE Spring Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh My, you both got the point Praveen Kumar M K and Surender Suri , understood, thank you so much ^_^
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Samer wrote:Oh My, you both got the point Praveen Kumar M K and Surender Suri , understood, thank you so much ^_^




Please QuoteYourSources -- this is *not* optional.
 
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It depends whether the code in the for loop executes first or the sysout statement in the main....
Although it is not sure in case of multithreading, but here sysout will be executed before the code enters the for loop...
 
Every snowflake is perfect and unique. And every snowflake contains a very tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic