This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark ""Going gone" ??" Watch ""Going gone" ??" New topic
Author

"Going gone" ??

sonir shah
Ranch Hand

Joined: Nov 01, 2001
Posts: 435
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

Joined: Jan 08, 2002
Posts: 22
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

Joined: Feb 26, 2001
Posts: 4442
    
    5

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.


    Junilu - [How to Ask Questions] [How to Answer Questions]
    Ragu Sivaraman
    Ranch Hand

    Joined: Jul 20, 2001
    Posts: 464
    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

    Joined: Dec 18, 2001
    Posts: 417
    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"
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: "Going gone" ??
     
    Similar Threads
    JQ+ going/gone question
    strings??
    JQplus Question ID :957921426790
    Precedence/Associativity
    Question related to array.