• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Could u explain this

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you please explain this example...How the variables are passed , how they are appended and assigned(y=x).

public class Google {
public static void main (String [] argv) {
StringBuffer a = new StringBuffer (�A�);
StringBuffer b = new StringBuffer (�B�);
execute (a,b);
system.out.printIn{a + �,� +b};
}
static void execute (StringBuffer x, StringBuffer y) {
x.append (y);
y = x;
}
}


Output is : code compiles fine and prints �AB,B".
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1). The API for StringBuffer.append(StringBuffer b) explains how it works.

2) the assignment y = x in method execute does nothing. Review argument passing in Java.
 
Supriya Nimakuri
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you please explain how the output is "AB,B"
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Supriya Nimakuri:
Can you please explain how the output is "AB,B"



The execute function will append the value of y to x (using the x.append method). The value of the a object will be "AB" and the value of the b object will be "B".

The y = x; line only affects the local y variable (which is a reference to the a object). The a variable is unaffected by this statement.
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Technically, all parameters in Java are pass-by-value.

This JavaFaq
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic