Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Stringbuffers

 
Rajani Sudhakar
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rajani Sudhakar
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi..

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

regards,
rajani.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Link problem now cleared up!
 
Angela lewis
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simply awesome
 
Swamy Nathan
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic