Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Strange behaviour ...., attention pls

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Test {
public static void main(String args[]) {
StringBuffer a = new StringBuffer("One");
StringBuffer b = new StringBuffer("Two");
Test.swap(a,b);
System.out.println("a = "+ a +" & b = " + b);
}
static void swap (StringBuffer a, StringBuffer b) {
a.append(" more");
b=a;
}
}
///ans is " a = one more & b = two "
can somebody throw light on the strange behaviour of the
"append()"
I expected the answer to be " one and two " but why this res?
Thanks in advance
satheesh

 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Satheesh,
What you're seeing is the result of the way Java handles passing parameters to methods.
When you pass 'a' to <code>swap()</code> you are passing a copy of the reference variable 'a' which points to the Object "one". The code <code>a.append("more")</code> tells the JVM to take the object pointed to by 'a' and add 'more' to it. Because 'a' points to the original object "one" is changed to "one more".
In the code <code>b = a</code>; 'b' is a copy of the reference variable pointing to the Object "two". You are telling the compiler to change the copy of the reference variable so it points to a different object; the original reference variable is not affected; when you're back in <code>main()</code> 'b' still points to the Object "two".
Hope that helps.
Jane
 
Satheesh Kumar
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jane for ur explanation, now I understood the way it works
satheesh
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is interesting question.
I always have questions.
See this code.
----------------------------------------------------
public class Test {
public static void main(String args[]) {
StringBuffer a = new StringBuffer("One");
StringBuffer b = new StringBuffer("Two");
Test.swap(a,b);
System.out.println("a = "+ a +" & b = " + b);
}
static void swap (StringBuffer a, StringBuffer b) {
a.append(" more");//copy of reference: line1
b.append(" confused");//copy of reference : line2
b=a;
System.out.println("In method swap: a="+a+" b="+b);
b.append(" less");// copy of reference : line3
}
}
---------------------------------------------------
it seems that method append() directly apply reference of a,
rather than the copy of reference. Right?
So my question is that suppose there are a collection of methods
apply directly the reference of objects. Can I get what they are?
Or where I can get them?
Another question is that what benefits are if we use pass reference by parameters. Can you give me your exampls?
Thanks
Simon
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both references (original and copy) point to the same object instance. Hence, calling append applies to the object and not the reference. Remember that a reference merely contains the location of the object. When you pass objects as arguments, you are passing a copy of the reference. You are NOT passing a copy of the object.
 
Won't you be my neighbor? - Fred Rogers. tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic