File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String/Thread question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String/Thread question" Watch "String/Thread question" New topic
Author

String/Thread question

David Samer
Ranch Hand

Joined: Feb 08, 2012
Posts: 49

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
Sheriff

Joined: Sep 28, 2004
Posts: 18716
    
  40

Please QuoteYourSources


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Guillaume Jourdan
Ranch Hand

Joined: Jul 24, 2012
Posts: 47

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

Joined: Jul 24, 2012
Posts: 47

So, if you use StringBuffer Object with your first code, you obtain a different result.
David Samer
Ranch Hand

Joined: Feb 08, 2012
Posts: 49

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

Joined: Jul 03, 2011
Posts: 256
Is the sname in nameTest, the same as the static variable sname? This should answer your question.
Surender Suri
Ranch Hand

Joined: May 28, 2010
Posts: 42
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.


Thanks,
Suri
David Samer
Ranch Hand

Joined: Feb 08, 2012
Posts: 49

Oh My, you both got the point Praveen Kumar M K and Surender Suri , understood, thank you so much ^_^
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18716
    
  40

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

Joined: Aug 11, 2012
Posts: 370
    
    1

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...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String/Thread question