Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sooooo Confused!!!

 
Shannon Sims
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have read that String are immutable? If so, then why can you do the following:
String s = "good";
s += "time";
In addition to this question, can someone please explain this problem I found on one of those mock exams (by the way, the answer that was given was incorrect). When I executed this code I got: vandeleur.
public class Tux extends Thread{
static String sName = "vandeleur";
public static void main(String argv[]){
Tux t = new Tux();
t.piggy( sName );
System.out.println( sName );
}
public void piggy(String sName){
sName = sName + " wiggy";
start();
}
public void run(){
for(int i=0;i < 4; i++)
{
//This line??? sName = sName + " " + i;
}
}
}
This line??? Since sName is a static String, why wouldn't sName have reflected the following: "vandeleur", "vandeleur 0", "vandeleur 0 1" "vandaleur 0 1 2" or "vandaleur 0 1 2 3"?
Ahhhhhhhhh!!! I'm so confused!!!
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

s isn't a String object; it's a String variable. It points to a String. It can point to different String objects at different times. The line 's += "time"' means to add the String pointed to by s to the String "time", then store the result back in the variable s. The String object "good" is unchanged, but you don't have a variable that points to it anymore.
The sample code you're showing is really a crazy mess; personally I think whoever wrote this sample question ought to have to crawl 20 paces on their knees through a room full of marbles. But in any case, look at the parameter name in the routine "piggy". The local parameter name takes precedence over the static variable name, so that the variable that is assigned to in piggy() is the parameter, not the static variable. That's why we don't see the "wiggy" part.
As far as the run() method goes: start() gets a thread moving, but run() hasn't necessarily even been called yet by the time start returns; so when the main thread prints out the value of the static variable -- which will indeed be reassigned from run() -- the reassignment hasn't happened yet; i.e., the assignments to sName are made (usually!) after the println() call.
(Actually in the code you show, you've got the line that does the assignment commented out -- I suspect that's just a typo).
It's possible, if highly unlikely, that you could see the strings you expected with the numbers on them, but 999,999 times out of a million, the println() will happen before the first assignment is made. Even if it doesn't, there are other issues with communication between threads that I haven't even addressed here.
 
Shannon Sims
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ernest,
Ah...ok, thanks for clarify the Tux code. That makes sense, the sName that was printed in "main" executed so quickly therefore never seeing the updated value of sName. Whew!!! Thank you so much for clarify that one, I thought I was going crazy!
Ok, I didn't have the code on hand when I sent my first post, so here it is....sorry Here's the code I wrote which confuses me because String are immutable correct? So why would this work?
public class StringTest{
public static void main( String args[] ){
StringTest t = new StringTest();
String j = new String( "Java rules" );
j.addOk( j );
}
public void addOk( String s ){
s += " OK!"; //see comment below *1
System.out.println(s);
}
}
*1 - now if a String is immutable, how can you modify a String object by appending " OK!" to the String object that was passed in.
Thanks so much for all the help!!! I'm taking my certification Thursday, so now I'm just going through all the mock exams compiling a bunch of questions that are questionable.
 
Amit KumarS
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi shannon,
Strings are immutable and that is true.. but as u say

now if a String is immutable, how can you modify a String object by appending " OK!" to the String object that was passed in.

this is possible because we don't modify the original string by doing

s += " OK!";
(as ernest has said!!)
what JVM does is it makes a new memory space where the value of s is copied and then the String " OK!" is appended to it. and then this memory refrence is given to 's' . and the old String "Java rules" will be not be referenced by any one so it become eliible for garbage collection.
i hope that is clear.. if still in doubt please revert back and one last thing i would like to draw your attention to in your code..
j.addOk( j ); will cause a compilation error.. i think you wrote 'j' by mistake instead of 't' as j is an object of String and String class has no method as addOk().
Thanks
Amit
 
Peter Kryszkiewicz
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try to see the difference between strings and string buffers. I used to stumble on them (in C++ AND Java, same concept) until I viewed strings like those blue recycling containers, filled with concrete. you could stack them (concatenate them) or throw them in the lake (garbage collect them) or stub your toe on them, but once you filled them with concrete (initialized them) that was that!
Now string buffers on the other hand (in general, for lots of languages) are like empty recycling boxes that you can fill and dump whenever you want, and they even get bigger if you try to overflow them.
I had to beat my head on this idea after forgetting that a string variable being assigned to a function return was likely to get me all kinds of error messages, cause the function was trying to refill a box of concrete.
Anyways, that analogy worked for me.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12200
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to look at some of the Campfire Stories on the main Javaranch page. specifically, the "cup size" story. about 1/2 way through the article, they start talking about "references".
I found it very helpful in understanding some of this stuff. In fact, i still occasionally go back and re-read those articles from time to time.
 
Shannon Sims
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AH OK!!! Got it now, got it (light just came on).
The variable "s" now has a new memory reference which now holds onto "Java rules OK!" instead of "Java rules". "Java rules" is no longer being referenced by another other variable. Right?
Thanks so much for the explanations everyone!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic