On Sep 5, 2:44 pm, "Krupinski Norbert"
<[EMAIL PROTECTED]> wrote:
> Great!
>
> But could you explain why the following modification:
>
> static void swap(StringBuffer firstStringBuffer, StringBuffer
> secondStringBuffer) {
> firstStringBuffer=secondStringBuffer; //MODIFICATION
> StringBuffer temporaryStringBuffer = new StringBuffer();
> temporaryStringBuffer.append(firstStringBuffer);
> firstStringBuffer.replace(0, secondStringBuffer.length(),
> secondStringBuffer.toString());
> secondStringBuffer.replace(0, temporaryStringBuffer.length(),
> temporaryStringBuffer.toString());
>
> }
>
> don't influence the result and change it into:
>
> Before swap - firstString = aaa secondString = bbb
> After swap - firstString = bbb secondString = bbb
>
It does not give the same result by me:
Before swap - firstString = aaa secondString = bbb
After swap - firstString = aaa secondString = bbb
That is easy to explain; in the swap method, you just make the
firstStringBuffer points to the secondStringBuffer, that changes its
internal value, but you have sort of clone of firstStringBuffer,
that's not the same one as in Main method. Hence when you assign the
value of firstStringBuffer to firstString in Main, you don't assign
the value of firstStringBuffer as it was in swap method, but as it is
in Main method. End result you don't change it.
Now, be aware that the equality symbol for String or the like is very
dangerous, it is always a copy of the String or the like, not the same
String or the like. That's why you see this behaviour.
You can observe the changes while debugging in Netbeans, put a break
on all lines except printing in swap, then put a break on both lines
after the swap call in Main, step into each step, and observe
carefully what happens to local variables in swap, then in Main. That
helps to understand you are not dealing with the same references
though they have the same names.
You may see what happens, this way:
static void swap(StringBuffer firstStringBuffer, StringBuffer
secondStringBuffer) {
System.out.println("firstStringBuffer: " +
firstStringBuffer.toString() +
" secondStringBuffer: " +
secondStringBuffer.toString());
firstStringBuffer = secondStringBuffer;
System.out.println("firstStringBuffer: " +
firstStringBuffer.toString()
+ " secondStringBuffer: " +
secondStringBuffer.toString());
StringBuffer temporaryStringBuffer = new StringBuffer();
temporaryStringBuffer.append(firstStringBuffer);
System.out.println("firstStringBuffer: " +
firstStringBuffer.toString()
+ " secondStringBuffer: " +
secondStringBuffer.toString()
+ " temporaryStringBuffer: " +
temporaryStringBuffer.toString());
firstStringBuffer.replace(0, secondStringBuffer.length(),
secondStringBuffer.toString());
System.out.println("firstStringBuffer: " +
firstStringBuffer.toString()
+ " secondStringBuffer: " +
secondStringBuffer.toString()
+ " temporaryStringBuffer: " +
temporaryStringBuffer.toString());
secondStringBuffer.replace(0, temporaryStringBuffer.length(),
temporaryStringBuffer.toString());
System.out.println("firstStringBuffer: " +
firstStringBuffer.toString()
+ " secondStringBuffer: " +
secondStringBuffer.toString()
+ " temporaryStringBuffer: " +
temporaryStringBuffer.toString());
}
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---