• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Stringbuffers

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi..

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

regards,
rajani.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Link problem now cleared up!
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Simply awesome
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Please do not shoot the fish in this barrel. But you can shoot at this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic