• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

call by reference ?

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is a question from Jargon test :-
in this when a and b are passed in swap method value of a changed by append and reflected in out put but value of b has not changed by b=a why ?
public class Test5 {
public static void main(String args[]) {
StringBuffer a = new StringBuffer("One");
StringBuffer b = new StringBuffer("Two");
Test5.swap(a,b);

System.out.println("a is "+ a +"\nb is " + b);
}
static void swap (StringBuffer a, StringBuffer b) {
a.append(" more");
b=a;
}
}
answer is one more and two
not one more one more
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In the case of object reference 'a', the append()
call is changing the object itself.
In the case of object reference 'b', the swap()
method is changing its local object reference
variable 'b'.
Since during a method invocation, a copy of the
reference is passed, the reference 'b' in main()
doesnot get changed. Also, the object refered by
'b' (in the main() method has not changed.
Hence the result.
Right? . I think it is.
Regds.
- satya
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI,


public class Test5 {
public static void main(String args[]) {
StringBuffer a = new StringBuffer("One");
StringBuffer b = new StringBuffer("Two");
Test5.swap(a,b);
System.out.println("a is "+ a +"\nb is " + b);
}
static void swap (StringBuffer a, StringBuffer b) {
a.append(" more");
b=a;
}
}


In the call to the swap(...) method the references to the StringBuffer's a and b are passed.
However int the swap(...) method, the contents of the StringBuffer a is modified ( remember the copy of the reference also points to the same location as in the main, hence the change is reflected in the main method for StringBuffer a now contains "One more"). Though the callers' copy is intact the contents have been modified for the StringBuffer a.
In case of b the callers' copy of the reference to StringBuffer b is intact hence prints out "Two". The assignment b=a is local only to the swap(...) method.
Hope this helps
Ajay

[This message has been edited by Ajay Kumar (edited May 15, 2000).]
 
reply
    Bookmark Topic Watch Topic
  • New Topic