Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String/Thread question

 
David Samer
Ranch Hand
Posts: 49
Eclipse IDE Opera Spring
  • Mark post as helpful
  • send pies
  • 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.
 
Henry Wong
author
Marshal
Pie
Posts: 20998
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
 
Guillaume Jourdan
Ranch Hand
Posts: 47
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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 Opera Spring
  • Mark post as helpful
  • send pies
  • 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)
 
Praveen Kumar M K
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the sname in nameTest, the same as the static variable sname? This should answer your question.
 
Surender Suri
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • 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 Opera Spring
  • Mark post as helpful
  • send pies
  • 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
Marshal
Pie
Posts: 20998
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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.
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • 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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic