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

"Going gone" ??

 
sonir shah
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What will be the output of the following program ?


Ans : It will print going gone.
The Explaination given for such an answer is:
The assignment of the string "gone" to s occurs after the first argument to print has been evaluated.
If evaluation of an argument expression completes abruptly, no part of any argument expression to its right appears to have been evaluated.
According to me, first the value of s is going, then in the next statement the value of s is gone.
But string values cannot be changed, right?
So how does we get a output of "going gone"
 
Chris Graham
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String values can be changed just as any other variable can be changed. The only way a String value can not be changed is if it is "final static" (or basically a constant).
This exercise got me thinking...and I was a bit confused because of what the following question from the Java Rule Round-up game tells me.
Java Rule Round-up - question #204
Question: When you pass an object reference as an argument to a method call, what gets passed?
Answer: A copy of the reference. You always get a copy of whatever is in the variable -- either a primitive or a reference. So for objects, you get a copy of the reference.
If what is being passed into this method is indeed copies of object references (which I think of as being memory locations), and these references are pointing to the objects, and not the actual value of the objects; wouldn't this mean that the since it's the same object, aren't the references the same (i.e. reference 1 evaluates to memory location 25, and reference 2 also evaluates to memory location 25, because both references point to object s)? And if the reference is the same, than there can only be one value stored in this reference, which would be "gone"?
If you can follow what I'm trying to say (it's late, and I might not be at the top of my game), please shead some light on this for me!
Thanks,
--Chris
 
Junilu Lacar
Bartender
Pie
Posts: 7770
62
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Chris Graham:
String values can be changed just as any other variable can be changed. The only way a String value can not be changed is if it is "final static" (or basically a constant).

Not true.
Strings are immutable: you can't change their values. You can, however, change the value of a String variable, which is what s is, i.e., make it point to another String.


When you write

this is what happens:
  • first parameter (String a) receives a copy of the value of s (a reference to the String constant "going")
  • second parameter (String b) receives a copy of the value of the expression (s = "gone") which happens to be a reference to the String constant "gone".


  • The print() method knows nothing about s or the fact that it was involved in the method call. All it is cares about is that it is getting two references to String objects in its parameters a and b.
     
    Ragu Sivaraman
    Ranch Hand
    Posts: 464
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    junilu is correct
    Strings are immutable, This ability gives the compiler to optimize them in a pool
    So String s = "Gone"; cannot be changed using the reference s... Any change you make with respect to the 's' may yield a seperate object but never changes what is in this binary blob...
    s= "gone"; //this is not a change this is total re-assignment so immutablilty is not a question here
    Hope it helps you
    ragu
     
    mark stone
    Ranch Hand
    Posts: 417
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    its very very simple.
    look at args for println method. it takes two args one is a and the other is b. right ? no confusion till here.
    now what just fill what a and b are ? they are String a and String b. right ? look at the method print. print(s, s = "gone"); ok ?
    s is going.
    and next arg is s="gone" . this needs to be evaluated first. so now you are assigning s equals gone. so the two args are going and gone.
    simple. right ? the trick was that you needed to evaluate the assignment and then use whatever value you get.

    Originally posted by sonir shah:
    What will be the output of the following program ?


    Ans : It will print going gone.
    The Explaination given for such an answer is:
    The assignment of the string "gone" to s occurs after the first argument to print has been evaluated.
    If evaluation of an argument expression completes abruptly, no part of any argument expression to its right appears to have been evaluated.
    According to me, first the value of s is going, then in the next statement the value of s is gone.
    But string values cannot be changed, right?
    So how does we get a output of "going gone"
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic