wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Stringbuffers 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 "Stringbuffers" Watch "Stringbuffers" New topic
Author

Stringbuffers

Rajani Sudhakar
Ranch Hand

Joined: Apr 28, 2004
Posts: 60
Hi..
This is a question from Dan Chisholm's single topic questions...


class MWC205 {
static void m1(StringBuffer s1) {
s1.append("B"); System.out.print(s1);
}
static void m2(StringBuffer s1) {
s1 = new StringBuffer("C"); System.out.print(s1);
}
public static void main(String[] s) {
StringBuffer s1 = new StringBuffer("A");
m1(s1); m2(s1);
System.out.print(s1);
}}

What is the result of attempting to compile and run the program?

a. Prints: AAA
b. Prints: ABCA
c. Prints: ABCAB
d. Prints: ABCABC
e. Prints: ABCAC
f. Prints: ABABCABC
g. Compile-time error
h. Run-time error
i. None of the above

Answer : C ..

My doubt is that in method 'm1' , 'AB' is printed..
in method 'm2' , 'c' is printed...

And Stringbuffers are immutable.
So why is 'AB' printed as a result of the print statement in the main()
method?

Why cant it be "C"?


Please anyone reply me..

Thanks in advance..

Regards..
rajani.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
First of all, StringBuffer objects are not immutable. Rather, String objects are immutable. That's why, if you have to do a lot of changing to a String, it is generally preferable to use a StringBuffer object rather than a String object.

Besides that, it's important to understand how parameters are passed in Java. Java is a "pass by value" language. That means that you'll always pass a copy of a variable to a new function. I'd suggest checking out this Flash Application I built a while ago. It might help you visualize what is happening.

Let me know if you're still confused after reviewing that.

Corey


SCJP Tipline, etc.
Rajani Sudhakar
Ranch Hand

Joined: Apr 28, 2004
Posts: 60
Hi..

I have seen your flash presentation..
My doubt is cleared..
Thankyou so much..

regards,
rajani.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Originally posted by Corey McGlone:

I'd suggest checking out this Flash Application I built a while ago. It might help you visualize what is happening.


Damn, Corey, that's really cool!


[Jess in Action][AskingGoodQuestions]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729

Not Found
The requested URL /mcglonec1978/javacert/javacert.html was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.



Eh? Whazzup?


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Hmm... All the links look like they're working to me. The full URL is:

http://www.geocities.com/mcglonec1978/javacert/javacert.html#param_passing
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Link problem now cleared up!
Angela lewis
Ranch Hand

Joined: Mar 01, 2004
Posts: 100
Simply awesome
Swamy Nathan
Ranch Hand

Joined: May 16, 2004
Posts: 187
Cool.
Should we say that the reference was passed by value?
Should we say that as long as the reference passed by value is not changed in its value by making it refer to another object it gives the illusion of objects being passed by reference?
Huh?
Am I right?
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Swamy Nathan:
Cool.
Should we say that the reference was passed by value?
Should we say that as long as the reference passed by value is not changed in its value by making it refer to another object it gives the illusion of objects being passed by reference?
Huh?
Am I right?


I suppose you could say that it "gives the illusion" of being passed by reference only because you're passing a reference. However, it's important to note that you're passing a copy of that reference and that, technically, makes it pass by value.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Stringbuffers
 
Similar Threads
Dan exam doubt 17
Please explain me the output in detail
Inheritance - Mock Question & Explanation on Dan Chisholm's website.
String buffer
StringBuffer question